Passed
Push — master ( 593336...bc2cf1 )
by Hannes
02:43
created

ListConsole   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 61
c 0
b 0
f 0
dl 0
loc 107
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A execute() 0 27 3
A configure() 0 54 1
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-19 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\giroapp\Console;
24
25
use byrokrat\giroapp\DependencyInjection\DonorQueryProperty;
26
use byrokrat\giroapp\Filter\FilterCollection;
27
use byrokrat\giroapp\Filter\CombinedFilter;
28
use byrokrat\giroapp\Formatter\FormatterCollection;
29
use byrokrat\giroapp\Sorter\SorterCollection;
30
use byrokrat\giroapp\Sorter\DescendingSorter;
31
use Symfony\Component\Console\Command\Command;
32
use Symfony\Component\Console\Input\InputOption;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Output\OutputInterface;
35
36
final class ListConsole implements ConsoleInterface
37
{
38
    use DonorQueryProperty;
39
40
    /** @var FilterCollection */
41
    private $filterCollection;
42
43
    /** @var FormatterCollection */
44
    private $formatterCollection;
45
46
    /** @var SorterCollection */
47
    private $sorterCollection;
48
49
    public function __construct(
50
        FilterCollection $filterCollection,
51
        FormatterCollection $formatterCollection,
52
        SorterCollection $sorterCollection
53
    ) {
54
        $this->filterCollection = $filterCollection;
55
        $this->formatterCollection = $formatterCollection;
56
        $this->sorterCollection = $sorterCollection;
57
    }
58
59
    public function configure(Command $command): void
60
    {
61
        $command->setName('list');
62
        $command->setAliases(['ls']);
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