Skip to content

Commit

Permalink
Merge pull request #32 from jaljo/feat/publish
Browse files Browse the repository at this point in the history
Feat/publish
  • Loading branch information
jaljo authored Jun 24, 2020
2 parents 7a1a91a + 9efcb60 commit 92c1b3f
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ security:

access_control:
- { path: '^/', roles: IS_AUTHENTICATED_ANONYMOUSLY, methods: [GET] }
- { path: '^/', roles: ROLE_ADMIN, methods: [POST] }
- { path: '^/', roles: ROLE_ADMIN, methods: [PATCH, POST] }
7 changes: 7 additions & 0 deletions config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ create_article:
path: /articles
methods: [POST]
controller: App\Application\Controller\ArticleController::create

publish_article:
path: /articles/{id}/publish
methods: [PATCH]
controller: App\Application\Controller\ArticleController::publish
requirements:
id: '[\d]+'
1 change: 1 addition & 0 deletions config/services/bus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ services:
- '@App\Domain\Command\Handler\ReadArticle'
- '@App\Domain\Command\Handler\ListArticles'
- '@App\Domain\Command\Handler\WriteArticle'
- '@App\Domain\Command\Handler\PublishArticle'
7 changes: 6 additions & 1 deletion config/services/command_handlers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ services:

App\Domain\Command\Handler\WriteArticle:
arguments:
- '@doctrine.orm.entity_manager'
- '@Doctrine\ORM\EntityManagerInterface'
- '@Ausi\SlugGenerator\SlugGenerator'

App\Domain\Command\Handler\PublishArticle:
arguments:
- '@Doctrine\ORM\EntityManagerInterface'
- '@App\Application\Repository\ArticleRepository'
2 changes: 1 addition & 1 deletion config/services/endpoints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ services:
App\Application\Controller\ArticleController:
arguments:
- '@App\Domain\Command\Bus\SimpleBus'
- '@form.factory'
- '@Symfony\Component\Form\FormFactoryInterface'
tags:
- 'controller.service_arguments'
16 changes: 16 additions & 0 deletions src/Application/Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,20 @@ public function create(Request $request): JsonResponse
Response::HTTP_CREATED
);
}

// @todo add swager doc here
public function publish(int $id): JsonResponse
{
try {
$article = $this->bus->executeCommand(new Command\PublishArticle($id));
}
catch (Exception $exception) {
return new JsonResponse(
["error" => $exception->getMessage()],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}

return new JsonResponse(new ArticleDefinition($article));
}
}
3 changes: 3 additions & 0 deletions src/Application/Controller/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace App\Application\Controller;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

interface Endpoint
{
public function list(): JsonResponse;

public function read(string $slug): JsonResponse;

public function create(Request $request): JsonResponse;
}
49 changes: 49 additions & 0 deletions src/Domain/Command/Handler/PublishArticle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Domain\Command\Handler;

use App\Domain\Model\Article;
use App\Domain\Command\PublishArticle as PublishArticleCommand;
use App\Domain\Exception\ResourceNotFoundException;
use App\Domain\Repository\ArticleRepository;
use Doctrine\Common\Persistence\ObjectManager;

class PublishArticle
{
/**
* @var ObjectManager
*/
private $manager;

/**
* @var ArticleRepository
*/
private $repository;

public function __construct(
ObjectManager $manager,
ArticleRepository $repository
) {
$this->manager = $manager;
$this->repository = $repository;
}

/**
* @throws ResourceNotFoundException
*/
public function __invoke(PublishArticleCommand $command): Article
{
$article = $this->repository->find($command->getId());

if (null === $article) {
throw new ResourceNotFoundException();
}

$article->publish();

$this->manager->persist($article);
$this->manager->flush();

return $article;
}
}
21 changes: 21 additions & 0 deletions src/Domain/Command/PublishArticle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Domain\Command;

class PublishArticle
{
/**
* @var int
*/
private $id;

public function __construct(int $id)
{
$this->id = $id;
}

public function getId(): int
{
return $this->id;
}
}
10 changes: 10 additions & 0 deletions src/Domain/Exception/ResourceNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Domain\Exception;

use Exception;

class ResourceNotFoundException extends Exception
{
protected $message = 'Resource not found.';
}
7 changes: 7 additions & 0 deletions src/Domain/Model/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public static function write(string $title, string $content, string $slug): self
return new self($title, $content, $slug);
}

public function publish(): self
{
$this->draft = false;

return $this;
}

public function getId(): int
{
return $this->id;
Expand Down

0 comments on commit 92c1b3f

Please sign in to comment.