Skip to content

Commit

Permalink
Finished the single generic command
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick in t Veld committed Sep 5, 2024
1 parent 37ac934 commit 4a1be91
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Action/Account/FetchUserSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace App\Action\Account;

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

final readonly class FetchUserSession implements ActionInterface
{
public function __construct(private UserRepository $userRepository) {}
public function __construct(private UserRepositoryInterface $userRepository) {}

public function __invoke(AggregateInterface $aggregator): void
{
Expand Down
23 changes: 17 additions & 6 deletions src/Command/AccountDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Command;

use App\ActionHandler\ActionHandlerInterface;
use App\Contract\Repository\MyFxBookRepositoryInterface;
use App\Contract\Repository\UserRepositoryInterface;
use App\Dto\Aggregator\AggregateRoot;
use App\Presentation\Output\TableInterface;
use App\Strategy\StrategyManagerInterface;
Expand All @@ -27,6 +29,8 @@ class AccountDataCommand extends Command
* @param StrategyManagerInterface<ActionHandlerInterface> $tableStrategy
*/
public function __construct(
private readonly MyFxBookRepositoryInterface $myFxBookRepository,
private readonly UserRepositoryInterface $userRepository,
private readonly StrategyManagerInterface $actionHandlerStrategy,
private readonly StrategyManagerInterface $tableStrategy,
) {
Expand All @@ -37,18 +41,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

/**
* @todo fetch all accounts
* @todo ask which account should be used
* @todo create a single account flow
*/

/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');

$user = $this->userRepository->findLatest();

if (is_null($user)) {
$io->error('No user found!');
}

$accounts = $this->myFxBookRepository->accounts($user->getSession());

Check failure on line 53 in src/Command/AccountDataCommand.php

View workflow job for this annotation

GitHub Actions / lint

Cannot call method getSession() on App\Entity\User|null.

Check failure on line 53 in src/Command/AccountDataCommand.php

View workflow job for this annotation

GitHub Actions / lint

Parameter #1 $session of method App\Contract\Repository\MyFxBookRepositoryInterface::accounts() expects string, string|null given.
$accountIds = array_map(static fn($a) => $a['id'], $accounts);

$question = new ChoiceQuestion('Select account ID', $accountIds);
$account = $helper->ask($input, $output, $question);
$dataType = $helper->ask($input, $output, new ChoiceQuestion('Please choose a data type: ', self::DATA_TYPES));

$aggregator = new AggregateRoot();
$aggregator->setSession($user->getSession());

Check failure on line 61 in src/Command/AccountDataCommand.php

View workflow job for this annotation

GitHub Actions / lint

Cannot call method getSession() on App\Entity\User|null.

Check failure on line 61 in src/Command/AccountDataCommand.php

View workflow job for this annotation

GitHub Actions / lint

Parameter #1 $session of method App\Dto\Aggregator\AggregateRoot::setSession() expects string, string|null given.
$aggregator->setAccounts([$account]);

/** @var ActionHandlerInterface $actionHandler */
$actionHandler = ($this->actionHandlerStrategy)($dataType);
Expand Down
4 changes: 2 additions & 2 deletions src/Command/LogoutCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace App\Command;

use App\Contract\Repository\UserRepositoryInterface;
use App\Event\LogoutEvent;
use App\Repository\UserRepository;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand All @@ -21,7 +21,7 @@ class LogoutCommand extends Command
{
public function __construct(
private readonly EventDispatcherInterface $eventDispatcher,
private readonly UserRepository $userRepository
private readonly UserRepositoryInterface $userRepository
) {
parent::__construct();
}
Expand Down
18 changes: 18 additions & 0 deletions src/Contract/Repository/UserRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Contract\Repository;

use App\Entity\User;

/**
* @method User|null find($id, $lockMode = null, $lockVersion = null)
* @method User|null findOneBy(array $criteria, array $orderBy = null)
* @method User[] findAll()
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
interface UserRepositoryInterface
{
public function findLatest(): ?User;
}
3 changes: 2 additions & 1 deletion src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Repository;

use App\Contract\Repository\UserRepositoryInterface;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
Expand All @@ -16,7 +17,7 @@
* @method User[] findAll()
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UserRepository extends ServiceEntityRepository
class UserRepository extends ServiceEntityRepository implements UserRepositoryInterface
{
public function __construct(ManagerRegistry $registry)
{
Expand Down

0 comments on commit 4a1be91

Please sign in to comment.