ListConsole   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 60
dl 0
loc 106
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 27 3
A __construct() 0 8 1
A configure() 0 53 1
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\DonorQueryProperty;
27
use byrokrat\giroapp\Filter\FilterCollection;
28
use byrokrat\giroapp\Filter\CombinedFilter;
29
use byrokrat\giroapp\Formatter\FormatterCollection;
30
use byrokrat\giroapp\Sorter\SorterCollection;
31
use byrokrat\giroapp\Sorter\DescendingSorter;
32
use Symfony\Component\Console\Command\Command;
33
use Symfony\Component\Console\Input\InputOption;
34
use Symfony\Component\Console\Input\InputInterface;
35
use Symfony\Component\Console\Output\OutputInterface;
36
37
final class ListConsole implements ConsoleInterface
38
{
39
    use DonorQueryProperty;
40
41
    /** @var FilterCollection */
42
    private $filterCollection;
43
44
    /** @var FormatterCollection */
45
    private $formatterCollection;
46
47
    /** @var SorterCollection */
48
    private $sorterCollection;
49
50
    public function __construct(
51
        FilterCollection $filterCollection,
52
        FormatterCollection $formatterCollection,
53
        SorterCollection $sorterCollection
54
    ) {
55
        $this->filterCollection = $filterCollection;
56
        $this->formatterCollection = $formatterCollection;
57
        $this->sorterCollection = $sorterCollection;
58
    }
59
60
    public function configure(Command $command): void
61
    {
62
        $command->setName('list');
63
        $command->setDescription('List donors');
64
        $command->setHelp('List donors in database');
65
66
        $command->addOption(
67
            'filter',
68
            null,
69
            InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
70
            sprintf(
71
                'Use donor filter, possible values: %s',
72
                implode(", ", $this->filterCollection->getItemKeys())
73
            )
74
        );
75
76
        $command->addOption(
77
            'filter-not',
78
            null,
79
            InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
80
            sprintf(
81
                'Use negated filter, possible values: %s',
82
                implode(", ", $this->filterCollection->getItemKeys())
83
            )
84
        );
85
86
        $command->addOption(
87
            'sorter',
88
            null,
89
            InputOption::VALUE_REQUIRED,
90
            sprintf(
91
                'Set donor sorter, possible values: %s',
92
                implode(", ", $this->sorterCollection->getItemKeys())
93
            ),
94
            ''
95
        );
96
97
        $command->addOption(
98
            'desc',
99
            null,
100
            InputOption::VALUE_NONE,
101
            'Sort donors in descending order'
102
        );
103
104
        $command->addOption(
105
            'format',
106
            null,
107
            InputOption::VALUE_REQUIRED,
108
            sprintf(
109
                'Set output format, possible values: %s',
110
                implode(", ", $this->formatterCollection->getItemKeys())
111
            ),
112
            'list'
113
        );
114
    }
115
116
    public function execute(InputInterface $input, OutputInterface $output): void
117
    {
118
        $filter = new CombinedFilter(
119
            ...array_merge(
120
                array_map([$this->filterCollection, 'getFilter'], (array)$input->getOption('filter')),
121
                array_map([$this->filterCollection, 'getNegatedFilter'], (array)$input->getOption('filter-not'))
122
            )
123
        );
124
125
        /** @var string */
126
        $sorterId = $input->getOption('sorter');
127
        $sorter = $this->sorterCollection->getSorter($sorterId);
128
129
        if ($input->getOption('desc')) {
130
            $sorter = new DescendingSorter($sorter);
131
        }
132
133
        /** @var string */
134
        $formatId = $input->getOption('format');
135
        $formatter = $this->formatterCollection->getFormatter($formatId);
136
        $formatter->initialize($output);
137
138
        foreach ($this->donorQuery->findAll()->filter($filter)->sort($sorter) as $donor) {
139
            $formatter->formatDonor($donor);
140
        }
141
142
        $formatter->finalize();
143
    }
144
}
145