Completed
Pull Request — master (#227)
by Beñat
03:56
created

TaskFixturesCommand::execute()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 27
nc 4
nop 2
dl 0
loc 40
rs 8.5806
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\TaskManager\Infrastructure\Ui\Cli\Command;
16
17
use Kreta\SharedKernel\Application\CommandBus;
18
use Kreta\SharedKernel\Application\QueryBus;
19
use Kreta\TaskManager\Application\Command\Project\Task\CreateTaskCommand;
20
use Kreta\TaskManager\Application\Query\Organization\OrganizationOfIdQuery;
21
use Kreta\TaskManager\Application\Query\Project\FilterProjectsQuery;
22
use Kreta\TaskManager\Domain\Model\Organization\UnauthorizedOrganizationActionException;
23
use Kreta\TaskManager\Domain\Model\Project\Task\TaskPriority;
24
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
class TaskFixturesCommand extends ContainerAwareCommand
29
{
30
    const TASK_PRIORITIES = [TaskPriority::LOW, TaskPriority::MEDIUM, TaskPriority::HIGH];
31
32
    private $commandBus;
33
    private $queryBus;
34
35
    public function __construct(CommandBus $commandBus, QueryBus $queryBus)
36
    {
37
        $this->commandBus = $commandBus;
38
        $this->queryBus = $queryBus;
39
        parent::__construct('kreta:task-manager:fixtures:tasks');
40
    }
41
42
    protected function execute(InputInterface $input, OutputInterface $output)
43
    {
44
        for ($i = 0; $i < 50; ++$i) {
45
            $userId = UserFixturesCommand::USER_IDS[array_rand(UserFixturesCommand::USER_IDS)];
46
47
            $this->queryBus->handle(
48
                new FilterProjectsQuery(
49
                    $userId,
50
                    0,
51
                    1
52
                ),
53
                $projects
54
            );
55
56
            foreach ($projects as $project) {
57
                try {
58
                    $this->queryBus->handle(
59
                        new OrganizationOfIdQuery(
60
                            $project['organization_id'],
61
                            $userId
62
                        ),
63
                        $organization
64
                    );
65
                } catch (UnauthorizedOrganizationActionException $exception) {
66
                    continue;
67
                }
68
                $this->commandBus->handle(
69
                    new CreateTaskCommand(
70
                        'Task ' . $i,
71
                        'The description of the task ' . $i,
72
                        $userId,
73
                        $organization['owners'][0]['user_id'],
74
                        self::TASK_PRIORITIES[array_rand(self::TASK_PRIORITIES)],
75
                        $project['id']
76
                    )
77
                );
78
            }
79
        }
80
        $output->writeln('Task population is successfully done');
81
    }
82
}
83