Skip to content

Commit

Permalink
fixed the way entities are fetched
Browse files Browse the repository at this point in the history
  • Loading branch information
Joris Langlois committed Jul 16, 2020
1 parent 35e12d2 commit e78715d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 21 deletions.
2 changes: 2 additions & 0 deletions config/services/command_handlers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ services:
App\Domain\Command\Handler\PublishArticle:
arguments:
- '@Doctrine\ORM\EntityManagerInterface'
- '@App\Application\Repository\ArticleRepository'

App\Domain\Command\Handler\EditArticle:
arguments:
- '@Doctrine\ORM\EntityManagerInterface'
- '@App\Application\Repository\ArticleRepository'
4 changes: 2 additions & 2 deletions src/Application/Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public function update(Request $request, int $id): JsonResponse
public function publish(int $id): JsonResponse
{
try {
$article = $this->bus->executeCommand(new Command\ReadArticle($id));
$this->bus->executeCommand(new Command\PublishArticle($article));
$publishArticle = new Command\PublishArticle($id);
$article = $this->bus->executeCommand($publishArticle);
}
catch (ResourceNotFoundException $exception) {
return new JsonResponse(null, Response::HTTP_NOT_FOUND);
Expand Down
10 changes: 5 additions & 5 deletions src/Domain/Command/EditArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
class EditArticle
{
/**
* @var Article
* @var int
*/
private $article;
private $id;

/**
* @var string
Expand All @@ -23,14 +23,14 @@ class EditArticle

public function __construct(Article $article)
{
$this->article = $article;
$this->id = $article->getId();
$this->title = $article->getTitle();
$this->content = $article->getContent();
}

public function getArticle(): Article
public function getId(): int
{
return $this->article;
return $this->id;
}

public function getTitle(): string
Expand Down
23 changes: 20 additions & 3 deletions src/Domain/Command/Handler/EditArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace App\Domain\Command\Handler;

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

class EditArticle
Expand All @@ -13,15 +15,30 @@ class EditArticle
*/
private $manager;

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

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

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

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

$article->edit(
$command->getTitle(),
$command->getContent()
Expand Down
19 changes: 16 additions & 3 deletions src/Domain/Command/Handler/PublishArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

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\Model\Article;
use App\Domain\Repository\ArticleRepository;
use Doctrine\Common\Persistence\ObjectManager;

class PublishArticle
Expand All @@ -14,18 +15,30 @@ class PublishArticle
*/
private $manager;

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

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

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

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

$article->publish();

$this->manager->persist($article);
Expand Down
14 changes: 6 additions & 8 deletions src/Domain/Command/PublishArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

namespace App\Domain\Command;

use App\Domain\Model\Article;

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

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

public function getArticle(): Article
public function getId(): int
{
return $this->article;
return $this->id;
}
}

0 comments on commit e78715d

Please sign in to comment.