Passed
Branch master (613786)
by Hannes
02:27
created

HistoryConsole::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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