HistoryConsole::execute()   B
last analyzed

Complexity

Conditions 7
Paths 18

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 15
nc 18
nop 2
dl 0
loc 24
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of byrokrat\giroapp.
5
 *
6
 * byrokrat\giroapp is free software: you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * byrokrat\giroapp is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * Copyright 2016-21 Hannes Forsgård
20
 */
21
22
declare(strict_types=1);
23
24
namespace byrokrat\giroapp\Console;
25
26
use byrokrat\giroapp\DependencyInjection\DonorEventStoreProperty;
27
use Symfony\Component\Console\Command\Command;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Input\InputInterface;
30
use Symfony\Component\Console\Output\OutputInterface;
31
32
final class HistoryConsole implements ConsoleInterface
33
{
34
    use Helper\DonorArgument;
35
    use DonorEventStoreProperty;
36
37
    private const TYPE_FILTER = [
38
        'attributes' => [
39
            'DONOR_ATTRIBUTE_REMOVED',
40
            'DONOR_ATTRIBUTE_UPDATED',
41
        ],
42
        'info' => [
43
            'DONOR_EMAIL_UPDATED',
44
            'DONOR_PHONE_UPDATED',
45
            'DONOR_POSTAL_ADDRESS_UPDATED',
46
            'DONOR_COMMENT_UPDATED',
47
            'DONOR_NAME_UPDATED',
48
        ],
49
        'state' => [
50
            'DONOR_ADDED',
51
            'DONOR_STATE_UPDATED',
52
            'DONOR_REMOVED',
53
            'DONOR_PAYER_NUMBER_UPDATED',
54
            'DONOR_AMOUNT_UPDATED',
55
        ],
56
        'transactions' => [
57
            'TRANSACTION_FAILED',
58
            'TRANSACTION_PERFORMED',
59
        ],
60
    ];
61
62
    public function configure(Command $command): void
63
    {
64
        $command
65
            ->setName('history')
66
            ->setDescription('Inspect donor history')
67
            ->setHelp('Display event log information associated with donor')
68
            ->addOption('attributes', null, InputOption::VALUE_NONE, 'Show attribute history')
69
            ->addOption('info', null, InputOption::VALUE_NONE, 'Show info history')
70
            ->addOption('state', null, InputOption::VALUE_NONE, 'Show state history')
71
            ->addOption('transactions', null, InputOption::VALUE_NONE, 'Show transaction history')
72
            ->addOption(
73
                'type',
74
                null,
75
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
76
                'Show entries matching custom type'
77
            )
78
        ;
79
80
        $this->configureDonorArgument($command);
81
    }
82
83
    public function execute(InputInterface $input, OutputInterface $output): void
84
    {
85
        $donor = $this->readDonor($input);
86
87
        $filterOnTypes = [];
88
89
        foreach (self::TYPE_FILTER as $option => $types) {
90
            if ($input->getOption($option)) {
91
                $filterOnTypes = array_merge($filterOnTypes, $types);
92
            }
93
        }
94
95
        if ($input->getOption('type')) {
96
            $filterOnTypes = array_merge($filterOnTypes, (array)$input->getOption('type'));
97
        }
98
99
        foreach ($this->donorEventStore->readEntriesForMandateKey($donor->getMandateKey()) as $entry) {
100
            if (empty($filterOnTypes) || in_array($entry->getType(), $filterOnTypes)) {
101
                $output->writeln(
102
                    sprintf(
103
                        "[%s] %-28s %s",
104
                        $entry->getDateTime()->format('Y-m-d H:i:s'),
105
                        $entry->getType(),
106
                        json_encode($entry->getData())
107
                    )
108
                );
109
            }
110
        }
111
    }
112
}
113