Completed
Push — master ( 7fe5eb...9bad48 )
by Beñat
08:54 queued 04:27
created

LoadFakeNotificationsCommand::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 16
nc 3
nop 2
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\Notifier\Infrastructure\Symfony\CliCommand;
16
17
use Kreta\Notifier\Application\Inbox\Notification\MarkAsReadNotificationCommand;
18
use Kreta\Notifier\Application\Inbox\Notification\MarkAsUnreadNotificationCommand;
19
use Kreta\Notifier\Application\Inbox\Notification\PublishNotificationCommand;
20
use Kreta\SharedKernel\Application\CommandBus;
21
use Kreta\SharedKernel\Infrastructure\Persistence\Fake\FakeDataCapabilities;
22
use Kreta\SharedKernel\Infrastructure\Persistence\Fake\UserFakeData;
23
use Predis\Client;
24
use Symfony\Component\Console\Command\Command;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
class LoadFakeNotificationsCommand extends Command
29
{
30
    use FakeDataCapabilities;
31
32
    private $commandBus;
33
    private $redis;
34
    private $userFakeData;
35
36 View Code Duplication
    public function __construct(CommandBus $commandBus, Client $redis, UserFakeData $userFakeData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        $this->commandBus = $commandBus;
39
        $this->redis = $redis;
40
        $this->userFakeData = $userFakeData;
41
42
        parent::__construct('kreta:notifier:inbox:notification:load');
43
    }
44
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $this->purgeDatabases();
48
49
        $i = 0;
50
        while ($i < $this->amount()) {
51
            $userId = $this->userFakeData->userOfIndex($i);
52
            $notificationId = $this->ids()[$i];
53
            $body = 'The notification body ' . $i;
54
55
            $receiveNotificationCommand = new PublishNotificationCommand($body, $userId, $notificationId);
56
            $this->commandBus->handle($receiveNotificationCommand);
57
58
            $readNotificationCommand = new MarkAsReadNotificationCommand($notificationId, $userId);
59
            $this->commandBus->handle($readNotificationCommand);
60
61
            if ($i % 2 === 0) {
62
                $unreadNotificationCommand = new MarkAsUnreadNotificationCommand($notificationId, $userId);
63
                $this->commandBus->handle($unreadNotificationCommand);
64
            }
65
            ++$i;
66
        }
67
68
        $output->writeln('The fake notifications are successfully loaded');
69
    }
70
71 View Code Duplication
    private function purgeDatabases() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        $i = 0;
74
        while ($i < $this->amount()) {
75
            $this->redis->del($this->type() . '-' . $this->dataOfIndex($i));
76
            ++$i;
77
        }
78
    }
79
80
    protected function type() : string
81
    {
82
        return 'notification';
83
    }
84
85
    protected function dataDir() : string
86
    {
87
        return __DIR__ . '/../../Persistence/Fake';
88
    }
89
}
90