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) |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
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.