Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ parameters:
- config/
- public/
- src/

ignoreErrors:
-
message: '#^Undefined variable: \$this$#'
paths:
- tests/*

reportUnmatchedIgnoredErrors: false
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Application\CommandHandler\Game;

use App\Application\Command\Game\CreateDeveloperCommand;
use App\Application\Fetcher\Game\DeveloperFetcher;
use App\Domain\Factory\Game\DeveloperFactory;
use App\Domain\Repository\Game\DeveloperRepositoryInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -15,13 +16,14 @@ class CreateDeveloperCommandHandler
{
public function __construct(
private readonly DeveloperRepositoryInterface $developerRepository,
private readonly DeveloperFetcher $developerFetcher,
private readonly LoggerInterface $logger,
) {
}

public function __invoke(CreateDeveloperCommand $command): void
{
$developer = $this->developerRepository->findByName($command->name);
$developer = $this->developerFetcher->findOneByName($command->name);

if (null !== $developer) {
return;
Expand All @@ -35,6 +37,10 @@ public function __invoke(CreateDeveloperCommand $command): void

try {
$this->developerRepository->save($developer);

/** @var string $developerName */
$developerName = $developer->getName();
$this->developerFetcher->deleteCacheName($developerName);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
Expand Down
23 changes: 13 additions & 10 deletions src/Application/CommandHandler/Game/CreateGameCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Application\Command\Game\CreateDeveloperCommand;
use App\Application\Command\Game\CreateGameCommand;
use App\Application\Command\Game\CreatePublisherCommand;
use App\Application\Fetcher\Game\GameFetcher;
use App\Application\Helper\MessageBusHelper;
use App\Application\Query\Game\GetDeveloperByNameQuery;
use App\Application\Query\Game\GetPublisherByNameQuery;
Expand All @@ -25,6 +26,7 @@ class CreateGameCommandHandler
{
public function __construct(
private readonly GameRepositoryInterface $gameRepository,
private readonly GameFetcher $gameFetcher,
private readonly PublisherRepositoryInterface $publisherRepository,
private readonly DeveloperRepositoryInterface $developerRepository,
private readonly MessageBusInterface $messageBus,
Expand All @@ -35,27 +37,30 @@ public function __construct(

public function __invoke(CreateGameCommand $command): void
{
$game = $this->gameRepository->getOneBySlugEnabledGame($command->slug);

if (null !== $game) {
return;
}

$publisher = null;
$developer = null;

if (null !== $command->publisher) {
/** @var ?Publisher $publisher */
$publisher = $this->publisherRepository->findByName(
$publisher = $this->publisherRepository->findOneByName(
$command->publisher->name,
);
}

if (null !== $command->developer) {
/** @var ?Developer $developer */
$developer = $this->developerRepository->findByName(
$developer = $this->developerRepository->findOneByName(
$command->developer->name,
);
}

if (
null !== $command->publisher
&& null === $publisher
) {
if (null !== $command->publisher && null === $publisher) {
$this->messageBus->dispatch(
new CreatePublisherCommand(
name: $command->publisher->name,
Expand All @@ -76,10 +81,7 @@ public function __invoke(CreateGameCommand $command): void
);
}

if (
null !== $command->developer
&& null === $developer
) {
if (null !== $command->developer && null === $developer) {
$this->messageBus->dispatch(
new CreateDeveloperCommand(
name: $command->developer->name,
Expand Down Expand Up @@ -112,6 +114,7 @@ public function __invoke(CreateGameCommand $command): void

try {
$this->gameRepository->save($game);
$this->gameFetcher->deleteCacheForSlug($command->slug);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function (): ?ApiGame {
$apiGame = $this->messageBusHelper->getContentFromEnvelope(
$gameEnvelope,
'Failed to fetch game from API',
ApiGame::class,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Application\Command\Game\CreatePublisherCommand;
use App\Application\Exception\Game\CannotCreatePublisherException;
use App\Application\Fetcher\Game\PublisherFetcher;
use App\Domain\Factory\Game\GamePublisherFactory;
use App\Domain\Repository\Game\PublisherRepositoryInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -16,6 +17,7 @@ class CreatePublisherCommandHandler
{
public function __construct(
private readonly PublisherRepositoryInterface $publisherRepository,
private readonly PublisherFetcher $publisherFetcher,
private readonly LoggerInterface $logger,
) {
}
Expand All @@ -25,7 +27,7 @@ public function __construct(
*/
public function __invoke(CreatePublisherCommand $command): void
{
$publisher = $this->publisherRepository->findByName($command->name);
$publisher = $this->publisherFetcher->findOneByName($command->name);

if (null !== $publisher) {
return;
Expand All @@ -39,6 +41,7 @@ public function __invoke(CreatePublisherCommand $command): void

try {
$this->publisherRepository->save($publisher);
$this->publisherFetcher->deleteCacheName($command->name);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
throw new CannotCreatePublisherException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Application\CommandHandler\Game;

use App\Application\Command\Game\UpdateDeveloperCommand;
use App\Application\Fetcher\Game\DeveloperFetcher;
use App\Domain\Model\Game\Developer;
use App\Domain\Repository\Game\DeveloperRepositoryInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -15,14 +16,18 @@ class UpdateDeveloperCommandHandler
{
public function __construct(
private readonly DeveloperRepositoryInterface $developerRepository,
private readonly DeveloperFetcher $developerFetcher,
private readonly LoggerInterface $logger,
) {
}

public function __invoke(UpdateDeveloperCommand $command): void
{
/** @var Developer $developer */
$developer = $this->developerRepository->findByApiId($command->developerApiId);
$developer = $this->developerFetcher->findOneByApiId(
$command->developerApiId,
$command->name,
);

if (null !== $developer) {
$developer->setName($command->name);
Expand All @@ -31,6 +36,7 @@ public function __invoke(UpdateDeveloperCommand $command): void

try {
$this->developerRepository->update($developer);
$this->developerFetcher->deleteCacheName($command->name);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public function __invoke(UpdateGameFromApiCommand $command): void
if (null !== $command->apiGame->getPublisher()) {
if (null === $publisher) {
try {
$this->messageBus->dispatch(new CreatePublisherCommand(
name: $command->apiGame->getPublisher()->name,
website: $command->apiGame->getPublisher()->website,
apiId: $command->apiGame->getPublisher()->apiId,
));
$this->messageBus->dispatch(
new CreatePublisherCommand(
name: $command->apiGame->getPublisher()->name,
website: $command->apiGame->getPublisher()->website,
apiId: $command->apiGame->getPublisher()->apiId,
),
);
} catch (ExceptionInterface) {
throw new CannotCreatePublisherException();
}
Expand All @@ -61,27 +63,33 @@ public function __invoke(UpdateGameFromApiCommand $command): void
$publisherApiId = $publisher->getApiId();

try {
$this->messageBus->dispatch(new UpdatePublisherCommand(
publisherApiId: $publisherApiId,
name: $command->apiGame->getPublisher()->name,
website: $command->apiGame->getPublisher()->website,
));
$this->messageBus->dispatch(
new UpdatePublisherCommand(
publisherApiId: $publisherApiId,
name: $command->apiGame->getPublisher()->name,
website: $command->apiGame->getPublisher()->website,
),
);
} catch (ExceptionInterface) {
throw new CannotUpdatePublisherException();
}
}

$publisher = $this->publisherRepository->findByApiId($command->apiGame->getPublisher()->apiId);
$publisher = $this->publisherRepository->findOneByApiId(
$command->apiGame->getPublisher()->apiId,
);
}

if (null !== $command->apiGame->getDeveloper()) {
if (null === $developer) {
try {
$this->messageBus->dispatch(new CreateDeveloperCommand(
name: $command->apiGame->getDeveloper()->name,
website: $command->apiGame->getDeveloper()->website,
apiId: $command->apiGame->getDeveloper()->apiId,
));
$this->messageBus->dispatch(
new CreateDeveloperCommand(
name: $command->apiGame->getDeveloper()->name,
website: $command->apiGame->getDeveloper()->website,
apiId: $command->apiGame->getDeveloper()->apiId,
),
);
} catch (ExceptionInterface) {
throw new CannotCreateDeveloperException();
}
Expand All @@ -90,17 +98,21 @@ public function __invoke(UpdateGameFromApiCommand $command): void
$developerApiId = $developer->getApiId();

try {
$this->messageBus->dispatch(new UpdateDeveloperCommand(
developerApiId: $developerApiId,
name: $command->apiGame->getDeveloper()->name,
website: $command->apiGame->getDeveloper()->website,
));
$this->messageBus->dispatch(
new UpdateDeveloperCommand(
developerApiId: $developerApiId,
name: $command->apiGame->getDeveloper()->name,
website: $command->apiGame->getDeveloper()->website,
),
);
} catch (ExceptionInterface) {
throw new CannotUpdateDeveloperException();
}
}

$developer = $this->developerRepository->findByApiId($command->apiGame->getDeveloper()->apiId);
$developer = $this->developerRepository->findOneByApiId(
$command->apiGame->getDeveloper()->apiId,
);
}

$command->game->setName($command->apiGame->getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Application\CommandHandler\Game;

use App\Application\Command\Game\UpdatePublisherCommand;
use App\Application\Fetcher\Game\PublisherFetcher;
use App\Domain\Model\Game\Publisher;
use App\Domain\Repository\Game\PublisherRepositoryInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -15,14 +16,18 @@ class UpdatePublisherCommandHandler
{
public function __construct(
private readonly PublisherRepositoryInterface $publisherRepository,
private readonly PublisherFetcher $publisherFetcher,
private readonly LoggerInterface $logger,
) {
}

public function __invoke(UpdatePublisherCommand $command): void
{
/** @var Publisher $publisher */
$publisher = $this->publisherRepository->findByApiId($command->publisherApiId);
$publisher = $this->publisherFetcher->findOneByApiId(
$command->publisherApiId,
$command->name,
);

if (null !== $publisher) {
$publisher->setName($command->name);
Expand All @@ -31,6 +36,7 @@ public function __invoke(UpdatePublisherCommand $command): void

try {
$this->publisherRepository->update($publisher);
$this->publisherFetcher->deleteCacheName($command->name);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace App\Application\CommandHandler\Report;

use App\Application\Command\Report\CreateReportFromDtoCommand;
use App\Application\Fetcher\Report\ReportFetcher;
use App\Application\Helper\CacheKeyBuilderHelper;
use App\Domain\Factory\Report\ReportFactory;
use App\Domain\Model\Game\Game;
use App\Domain\Repository\Game\GameRepositoryInterface;
Expand All @@ -18,6 +20,7 @@ class CreateReportFromDtoCommandHandler
public function __construct(
private readonly ReportRepositoryInterface $reportRepository,
private readonly GameRepositoryInterface $gameRepository,
private readonly ReportFetcher $reportFetcher,
) {
}

Expand Down Expand Up @@ -46,5 +49,8 @@ public function __invoke(CreateReportFromDtoCommand $command): void
);

$this->reportRepository->save($newReport);
$this->reportFetcher->deleteCache(
CacheKeyBuilderHelper::build(ReportFetcher::REPORTS_CACHE_KEY, $command->formInputDto->gameSlug)
);
}
}
13 changes: 13 additions & 0 deletions src/Application/Enum/CacheDurationEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Application\Enum;

enum CacheDurationEnum: int
{
case ONE_HOUR = 3600;
case SIX_HOURS = 21600;
case ONE_DAY = 86400;
case THREE_DAYS = 259200;
}
Loading