Completed
Push — master ( d94ed5...244927 )
by Arkadiusz
14:18 queued 12:13
created

ShowRecordedEvents::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 2
1
<?php
2
3
namespace SyliusCart\CartBundle\Command;
4
5
use Broadway\Domain\DomainMessage;
6
use Broadway\EventStore\EventStoreInterface;
7
use Broadway\UuidGenerator\UuidGeneratorInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Helper\Table;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Question\Question;
13
14
/**
15
 * @author Arkadiusz Krakowiak <[email protected]>
16
 */
17
final class ShowRecordedEvents extends Command
18
{
19
    /**
20
     * @var EventStoreInterface
21
     */
22
    private $eventStore;
23
24
    /**
25
     * @var UuidGeneratorInterface
26
     */
27
    private $uuidGenerator;
28
29
    /**
30
     * @param EventStoreInterface $eventStore
31
     * @param UuidGeneratorInterface $uuidGenerator
32
     */
33
    public function __construct(EventStoreInterface $eventStore, UuidGeneratorInterface $uuidGenerator)
34
    {
35
        $this->eventStore = $eventStore;
36
        $this->uuidGenerator = $uuidGenerator;
37
38
        parent::__construct();
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function configure()
45
    {
46
        $this
47
            ->setName('sylius:event-store:load')
48
            ->setDescription('Debug command to see all recorded events.')
49
        ;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $helper = $this->getHelper('question');
58
        $cartId = $helper->ask(
59
            $input,
60
            $output,
61
            new Question(
62
                sprintf('Cart id (%s): ', $this->uuidGenerator->generate()),
63
                $this->uuidGenerator->generate()
64
            )
65
        );
66
67
        $domainEventStream = $this->eventStore->load($cartId);
68
69
        /** @var DomainMessage $event */
70
        foreach ($domainEventStream as $event) {
71
            $table = new Table($output);
72
            $table
73
                ->setHeaders(['Recorded on', 'Type', 'Payload'])
74
                ->setRows([[$event->getRecordedOn()->toString(), $event->getType(), $this->implodeRecursively($event->getPayload()->serialize())]])
75
            ;
76
77
            $table->render();
78
        }
79
    }
80
81
    /**
82
     * @param array $payload
83
     *
84
     * @return string
85
     */
86
    private function implodeRecursively(array $payload): string
87
    {
88
        foreach ($payload as $key => $data) {
89
            if (is_array($data)) {
90
                try {
91
                    $payload[$key] = implode(' ', $data);
92
                } catch (\Exception $e) {
93
                    $payload[$key] = $this->implodeRecursively($payload[$key]);
0 ignored issues
show
Documentation introduced by
$payload[$key] is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
                }
95
            }
96
        }
97
98
        return implode(' ', $payload);
99
    }
100
}
101