ConfiguringEnvironment::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 11
dl 0
loc 24
rs 9.9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\Plugin;
25
26
use byrokrat\giroapp\CommandBus\CommandBusInterface;
27
use byrokrat\giroapp\Config\ConfigManager;
28
use byrokrat\giroapp\Console\ConsoleInterface;
29
use byrokrat\giroapp\Console\SymfonyCommandAdapter;
30
use byrokrat\giroapp\Db\DonorQueryInterface;
31
use byrokrat\giroapp\Db\DriverFactoryCollection;
32
use byrokrat\giroapp\Db\DriverFactoryInterface;
33
use byrokrat\giroapp\DependencyInjection\CommandBusProperty;
34
use byrokrat\giroapp\DependencyInjection\DispatcherProperty;
35
use byrokrat\giroapp\Exception\UnsupportedVersionException;
36
use byrokrat\giroapp\Filter\FilterCollection;
37
use byrokrat\giroapp\Filter\FilterInterface;
38
use byrokrat\giroapp\Formatter\FormatterCollection;
39
use byrokrat\giroapp\Formatter\FormatterInterface;
40
use byrokrat\giroapp\Sorter\SorterCollection;
41
use byrokrat\giroapp\Sorter\SorterInterface;
42
use byrokrat\giroapp\Status\StatisticInterface;
43
use byrokrat\giroapp\Status\StatisticsManager;
44
use byrokrat\giroapp\Version;
45
use byrokrat\giroapp\Xml\CompilerPassInterface;
46
use byrokrat\giroapp\Xml\XmlMandateCompiler;
47
use Composer\Semver\Semver;
48
use Symfony\Component\Console\Application;
49
use Psr\EventDispatcher\ListenerProviderInterface;
50
use Fig\EventDispatcher\AggregateProvider;
51
use Crell\Tukio\OrderedProviderInterface;
52
use Psr\Log\LoggerInterface;
53
54
final class ConfiguringEnvironment implements EnvironmentInterface
55
{
56
    use CommandBusProperty;
57
    use DispatcherProperty;
58
59
    /** @var LoggerInterface */
60
    private $logger;
61
62
    /** @var DonorQueryInterface */
63
    private $donorQuery;
64
65
    /** @var AggregateProvider */
66
    private $aggregateProvider;
67
68
    /** @var OrderedProviderInterface */
69
    private $orderedProvider;
70
71
    /** @var DriverFactoryCollection */
72
    private $dbDriverFactoryCollection;
73
74
    /** @var FilterCollection */
75
    private $filterCollection;
76
77
    /** @var FormatterCollection */
78
    private $formatterCollection;
79
80
    /** @var SorterCollection */
81
    private $sorterCollection;
82
83
    /** @var StatisticsManager */
84
    private $statisticsManager;
85
86
    /** @var ConfigManager */
87
    private $configManager;
88
89
    /** @var ConsoleInterface[] */
90
    private $consoleCommands = [];
91
92
    /** @var XmlMandateCompiler */
93
    private $xmlMandateCompiler;
94
95
    public function __construct(
96
        LoggerInterface $logger,
97
        DonorQueryInterface $donorQuery,
98
        AggregateProvider $aggregateProvider,
99
        OrderedProviderInterface $orderedProvider,
100
        DriverFactoryCollection $dbDriverFactoryCollection,
101
        FilterCollection $filterCollection,
102
        FormatterCollection $formatterCollection,
103
        SorterCollection $sorterCollection,
104
        StatisticsManager $statisticsManager,
105
        ConfigManager $configManager,
106
        XmlMandateCompiler $xmlMandateCompiler
107
    ) {
108
        $this->logger = $logger;
109
        $this->donorQuery = $donorQuery;
110
        $this->aggregateProvider = $aggregateProvider;
111
        $this->orderedProvider = $orderedProvider;
112
        $this->dbDriverFactoryCollection = $dbDriverFactoryCollection;
113
        $this->filterCollection = $filterCollection;
114
        $this->formatterCollection = $formatterCollection;
115
        $this->sorterCollection = $sorterCollection;
116
        $this->statisticsManager = $statisticsManager;
117
        $this->configManager = $configManager;
118
        $this->xmlMandateCompiler = $xmlMandateCompiler;
119
    }
120
121
    public function assertApiVersion(ApiVersionConstraint $constraint): void
122
    {
123
        if (!Semver::satisfies(Version::getSemverVersion(), $constraint->getConstraint())) {
124
            throw new UnsupportedVersionException(sprintf(
125
                'API version %s does not satisfy constraint %s in %s',
126
                Version::getSemverVersion(),
127
                $constraint->getConstraint(),
128
                $constraint->getName()
129
            ));
130
        }
131
    }
132
133
    public function readConfig(string $key): string
134
    {
135
        return $this->configManager->getConfig($key)->getValue();
136
    }
137
138
    public function getLogger(): LoggerInterface
139
    {
140
        return $this->logger;
141
    }
142
143
    public function getCommandBus(): CommandBusInterface
144
    {
145
        return $this->commandBus;
146
    }
147
148
    public function getDonorQuery(): DonorQueryInterface
149
    {
150
        return $this->donorQuery;
151
    }
152
153
    public function registerPlugin(PluginInterface $plugin): void
154
    {
155
        $plugin->loadPlugin($this);
156
    }
157
158
    public function registerConsoleCommand(ConsoleInterface $consoleCommand): void
159
    {
160
        $this->consoleCommands[] = $consoleCommand;
161
    }
162
163
    public function registerDatabaseDriver(DriverFactoryInterface $driverFactory): void
164
    {
165
        $this->dbDriverFactoryCollection->addDriverFactory($driverFactory);
166
    }
167
168
    public function registerListener(callable $listener, int $priority = 0): void
169
    {
170
        $this->orderedProvider->addListener($listener, $priority);
171
    }
172
173
    public function registerListenerProvider(ListenerProviderInterface $provider): void
174
    {
175
        $this->aggregateProvider->addProvider($provider);
176
    }
177
178
    public function registerDonorFilter(FilterInterface $donorFilter): void
179
    {
180
        $this->filterCollection->addFilter($donorFilter);
181
    }
182
183
    public function registerDonorFormatter(FormatterInterface $donorFormatter): void
184
    {
185
        $this->formatterCollection->addFormatter($donorFormatter);
186
    }
187
188
    public function registerDonorSorter(SorterInterface $donorSorter): void
189
    {
190
        $this->sorterCollection->addSorter($donorSorter);
191
    }
192
193
    public function registerStatistic(StatisticInterface $statistic): void
194
    {
195
        $this->statisticsManager->addStatistic($statistic);
196
    }
197
198
    public function registerXmlMandateCompilerPass(CompilerPassInterface $compilerPass): void
199
    {
200
        $this->xmlMandateCompiler->addCompilerPass($compilerPass);
201
    }
202
203
    public function configureApplication(Application $application): void
204
    {
205
        foreach ($this->consoleCommands as $consoleCommand) {
206
            $application->add(new SymfonyCommandAdapter($consoleCommand, $this, $this->dispatcher));
207
        }
208
    }
209
}
210