Passed
Push — master ( 2a8eac...e4d347 )
by Hannes
01:51
created

ConfiguringEnvironment::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 8
dl 0
loc 18
rs 10
c 0
b 0
f 0

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
 * 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\Plugin;
24
25
use byrokrat\giroapp\Console\Adapter;
26
use byrokrat\giroapp\Console\CommandInterface;
27
use byrokrat\giroapp\Config\ConfigManager;
28
use byrokrat\giroapp\Exception\UnsupportedVersionException;
29
use byrokrat\giroapp\Filter\FilterCollection;
30
use byrokrat\giroapp\Filter\FilterInterface;
31
use byrokrat\giroapp\Formatter\FormatterCollection;
32
use byrokrat\giroapp\Formatter\FormatterInterface;
33
use byrokrat\giroapp\Sorter\SorterCollection;
34
use byrokrat\giroapp\Sorter\SorterInterface;
35
use byrokrat\giroapp\State\StateCollection;
36
use byrokrat\giroapp\State\StateInterface;
37
use byrokrat\giroapp\Xml\XmlFormInterface;
38
use byrokrat\giroapp\Xml\XmlFormTranslator;
39
use Composer\Semver\Semver;
40
use Symfony\Component\Console\Application;
41
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
42
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
43
44
final class ConfiguringEnvironment implements EnvironmentInterface
45
{
46
    /**
47
     * @var ApiVersion
48
     */
49
    private $apiVersion;
50
51
    /**
52
     * @var EventDispatcherInterface
53
     */
54
    private $dispatcher;
55
56
    /**
57
     * @var FilterCollection
58
     */
59
    private $filterCollection;
60
61
    /**
62
     * @var FormatterCollection
63
     */
64
    private $formatterCollection;
65
66
    /**
67
     * @var SorterCollection
68
     */
69
    private $sorterCollection;
70
71
    /**
72
     * @var StateCollection
73
     */
74
    private $stateCollection;
75
76
    /**
77
     * @var ConfigManager
78
     */
79
    private $configManager;
80
81
    /**
82
     * @var XmlFormTranslator
83
     */
84
    private $xmlFormTranslator;
85
86
    /**
87
     * @var CommandInterface[]
88
     */
89
    private $commands = [];
90
91
    public function __construct(
92
        ApiVersion $apiVersion,
93
        EventDispatcherInterface $dispatcher,
94
        FilterCollection $filterCollection,
95
        FormatterCollection $formatterCollection,
96
        SorterCollection $sorterCollection,
97
        StateCollection $stateCollection,
98
        ConfigManager $configManager,
99
        XmlFormTranslator $xmlFormTranslator
100
    ) {
101
        $this->apiVersion = $apiVersion;
102
        $this->dispatcher = $dispatcher;
103
        $this->filterCollection = $filterCollection;
104
        $this->formatterCollection = $formatterCollection;
105
        $this->sorterCollection = $sorterCollection;
106
        $this->stateCollection = $stateCollection;
107
        $this->configManager = $configManager;
108
        $this->xmlFormTranslator = $xmlFormTranslator;
109
    }
110
111
    public function assertApiVersion(ApiVersionConstraint $constraint): void
112
    {
113
        if (!Semver::satisfies($this->apiVersion->getVersion(), $constraint->getConstraint())) {
114
            throw new UnsupportedVersionException(sprintf(
115
                'API version %s does not satisfy constraint %s in %s',
116
                $this->apiVersion->getVersion(),
117
                $constraint->getConstraint(),
118
                $constraint->getName()
119
            ));
120
        }
121
    }
122
123
    public function readConfig(string $key): string
124
    {
125
        return $this->configManager->getConfig($key)->getValue();
126
    }
127
128
    public function registerCommand(CommandInterface $command): void
129
    {
130
        $this->commands[] = $command;
131
    }
132
133
    public function registerSubscriber(EventSubscriberInterface $subscriber): void
134
    {
135
        $this->dispatcher->addSubscriber($subscriber);
136
    }
137
138
    public function registerDonorFilter(FilterInterface $donorFilter): void
139
    {
140
        $this->filterCollection->addFilter($donorFilter);
141
    }
142
143
    public function registerDonorFormatter(FormatterInterface $donorFormatter): void
144
    {
145
        $this->formatterCollection->addFormatter($donorFormatter);
146
    }
147
148
    public function registerDonorSorter(SorterInterface $donorSorter): void
149
    {
150
        $this->sorterCollection->addSorter($donorSorter);
151
    }
152
153
    public function registerDonorState(StateInterface $donorState): void
154
    {
155
        $this->stateCollection->addState($donorState);
156
    }
157
158
    public function registerXmlForm(XmlFormInterface $xmlForm): void
159
    {
160
        $this->xmlFormTranslator->addXmlForm($xmlForm);
161
    }
162
163
    public function configureApplication(Application $application): void
164
    {
165
        foreach ($this->commands as $command) {
166
            $application->add(new Adapter($command, $this->dispatcher));
167
        }
168
    }
169
}
170