Skip to content

Commit

Permalink
Created exceptions for the actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick in t Veld committed Apr 25, 2024
1 parent 20ee59f commit 4d49833
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Action/Account/FetchAccounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Dto\Aggregator\AggregateInterface;
use App\Event\CreateAccountEvent;
use App\Event\UpdateAccountEvent;
use App\Exception\AccountNotFoundException;
use App\Repository\AccountRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

Expand All @@ -30,7 +31,7 @@ public function __invoke(AggregateInterface $aggregator): void
}

if (empty($myFxBookAccounts)) {
throw new \Exception('No accounts found!');
throw new AccountNotFoundException();
}

$accounts = $this->accountRepository->findAll();
Expand Down
3 changes: 2 additions & 1 deletion src/Action/Account/FetchTradingAccounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Contract\Repository\MyFxBookRepositoryInterface;
use App\Dto\Aggregator\AggregateInterface;
use App\Event\CreateAccountEvent;
use App\Exception\AccountNotFoundException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

final readonly class FetchTradingAccounts implements ActionInterface
Expand All @@ -23,7 +24,7 @@ public function __invoke(AggregateInterface $aggregator): void
$accounts = $this->myFxBookRepository->accounts($aggregator->getSession());

if (count($accounts) === 0) {
throw new \Exception('No accounts found');
throw new AccountNotFoundException();
}

foreach ($accounts as $account) {
Expand Down
3 changes: 2 additions & 1 deletion src/Action/Account/FetchUserSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Action\ActionInterface;
use App\Dto\Aggregator\AggregateInterface;
use App\Exception\SessionNotFoundException;
use App\Repository\UserRepository;

final readonly class FetchUserSession implements ActionInterface
Expand All @@ -19,7 +20,7 @@ public function __invoke(AggregateInterface $aggregator): void
$user = $this->userRepository->findLatest();

if (null === $user || null === $user->getSession()) {
throw new \Exception('Could not find a user session');
throw new SessionNotFoundException();
}

$aggregator->setSession($user->getSession());
Expand Down
3 changes: 2 additions & 1 deletion src/Action/Download/FileDownloadAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Action\ActionInterface;
use App\Dto\Aggregator\AggregateInterface;
use App\Error\InstanceError;
use App\FileSystem\File;
use App\Manager\FileDownloadManager;

Expand All @@ -18,7 +19,7 @@ public function __construct(private FileDownloadManager $fileDownloadManager)
public function __invoke(AggregateInterface $aggregator): void
{
if (($aggregator->getData() instanceof File) === false) {
throw new \RuntimeException('Data should be an instance of class ' . File::class);
throw new InstanceError(File::class);
}

($this->fileDownloadManager)($aggregator->getData());
Expand Down
13 changes: 13 additions & 0 deletions src/Error/InstanceError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Error;

class InstanceError extends \TypeError
{
public function __construct(string $instance)
{
parent::__construct(sprintf('Should be an instance of class %s', $instance));
}
}
13 changes: 13 additions & 0 deletions src/Exception/AccountNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Exception;

final class AccountNotFoundException extends \Exception
{
public function __construct()
{
parent::__construct('Account not found!');
}
}
13 changes: 13 additions & 0 deletions src/Exception/SessionNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Exception;

class SessionNotFoundException extends \Exception
{
public function __construct()
{
parent::__construct('Session not found!');
}
}

0 comments on commit 4d49833

Please sign in to comment.