Completed
Pull Request — master (#104)
by Nicolas
03:17
created

Application::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Karma;
6
7
use Karma\Configuration\Reader;
8
use Karma\Configuration\Parser;
9
use Gaufrette\Filesystem;
10
use Gaufrette\Adapter\Local;
11
use Gaufrette\Adapter\Cache;
12
use Karma\FormatterProviders\ProfileProvider;
13
use Karma\Generator\NameTranslators\FilePrefixTranslator;
14
use Karma\Generator\VariableProvider;
15
use Karma\Generator\ConfigurationFileGenerators\YamlGenerator;
16
use Karma\Filesystem\Adapters\MultipleAdapter;
17
use Karma\Filesystem\Adapters\SingleLocalFile;
18
use Pimple\Container;
19
20
class Application extends Container
21
{
22
    const
23
        VERSION = '7.4.0',
24
        DEFAULT_DISTFILE_SUFFIX = '-dist',
25
        DEFAULT_CONF_DIRECTORY = 'env',
26
        DEFAULT_MASTER_FILE = 'master.conf',
27
        BACKUP_SUFFIX = '~',
28
        FINDER_CACHE_DIRECTORY = 'cache/karma',
29
        FINDER_CACHE_DURATION = 86400,
30
        PROFILE_FILENAME = '.karma';
31
32 41
    public function __construct()
33
    {
34 41
        parent::__construct();
35
36 41
        $this->initializeParameters();
0 ignored issues
show
Unused Code introduced by
The call to the method Karma\Application::initializeParameters() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
37
38 41
        $this->initializeConfiguration();
39 41
        $this->initializeProfile();
40 41
        $this->initializeFinder();
41 41
        $this->initializeSourceFileSystem();
42
43 41
        $this->initializeServices();
44 41
    }
45
46 41
    private function initializeParameters(): void
47
    {
48 41
        $this['configuration.path'] = 'conf';
49 41
        $this['configuration.masterFile'] = 'master.conf';
50
51 41
        $this['sources.path'] = ['src'];
52 41
        $this['target.path'] = null;
53 41
        $this['hydrator.allowNonDistFilesOverwrite'] = false;
54
55 41
        $this['distFiles.suffix'] = '-dist';
56 41
    }
57
58 41
    private function initializeConfiguration(): void
59
    {
60 41
        $this['configuration.fileSystem.adapter'] = $this->factory(function(Container $c) {
61
            return new Local($c['configuration.path']);
62 41
        });
63
64 41
        $this['configuration.fileSystem'] = $this->factory(function(Container $c) {
65 32
            return new Filesystem($c['configuration.fileSystem.adapter']);
66 41
        });
67
68 41
        $this['parser'] = function(Container $c) {
69 32
            $parser = new Parser($c['configuration.fileSystem']);
70
71 32
            $parser->enableIncludeSupport()
72 32
                ->enableExternalSupport()
73 32
                ->enableGroupSupport()
74 32
                ->setLogger($c['logger'])
75 32
                ->parse($c['configuration.masterFile']);
76
77 32
            return $parser;
78
        };
79
80 41
        $this['configuration'] = function(Container $c) {
81 31
            $parser = $c['parser'];
82
83 31
            return new Reader(
84 31
                $parser->getVariables(),
85 31
                $parser->getExternalVariables(),
86 31
                $parser->getGroups(),
87 31
                $parser->getDefaultEnvironmentsForGroups()
88
            );
89
        };
90 41
    }
91
92 41
    private function initializeProfile(): void
93
    {
94 41
        $this['profile.fileSystem.adapter'] = $this->factory(function(Container $c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95 2
            return new Local(getcwd());
96 41
        });
97
98 41
        $this['profile.fileSystem'] = $this->factory(function(Container $c) {
99 38
            return new Filesystem($c['profile.fileSystem.adapter']);
100 41
        });
101
102 41
        $this['profile'] = function(Container $c) {
103 38
            return new ProfileReader($c['profile.fileSystem']);
104
        };
105 41
    }
106
107 41
    private function initializeSourceFileSystem(): void
108
    {
109 41
        $this['sources.fileSystem.adapter'] = $this->factory(function(Container $c) {
110
111 2
            $paths = $c['sources.path'];
112
113 2
            if(! is_array($paths))
114
            {
115
                $paths = [$paths];
116
            }
117
118 2
            $adapter = new MultipleAdapter();
119
120 2
            foreach($paths as $path)
121
            {
122 2
                $localAdapter = new Local($path);
123
124 2
                if(is_file($path))
125
                {
126
                    $filename = basename($path);
127
                    $path = realpath(dirname($path));
128
                    $localAdapter = new SingleLocalFile($filename, new Local($path));
129
                }
130
131 2
                $adapter->mount($path, $localAdapter);
132
            }
133
134 2
            return $adapter;
135 41
        });
136
137 41
        $this['target.fileSystem.adapter'] = $this->factory(function(Container $c) {
138
139 3
            if(! empty($c['target.path']))
140
            {
141
                $c['hydrator.allowNonDistFilesOverwrite'] = true;
142
143
                return new Local($c['target.path']);
144
            }
145
146 3
            return $this['sources.fileSystem.adapter'];
147 41
        });
148
149 41
        $this['target.fileSystem'] = $this->factory(function(Container $c) {
150 11
            return new Filesystem($c['target.fileSystem.adapter']);
151 41
        });
152
153 41
        $this['sources.fileSystem'] = $this->factory(function(Container $c) {
154 12
            return new Filesystem($c['sources.fileSystem.adapter']);
155 41
        });
156
157 41
        $this['generate.sources.fileSystem.adapter'] = $this->factory(function(Container $c) {
158
159 1
            $sources = $c['sources.path'];
160
161 1
            if(count($sources) > 1)
162
            {
163
                throw new \InvalidArgumentException("Generate command does not allow multiple source paths");
164
            }
165
166 1
            return new Local(
167 1
                array_shift($sources)
168
            );
169 41
        });
170
171 41
        $this['generate.sources.fileSystem'] = $this->factory(function(Container $c) {
172 6
            return new Filesystem($c['generate.sources.fileSystem.adapter']);
173 41
        });
174
175 41
        $this['sources.fileSystem.finder'] = $this->factory(function(Container $c) {
176 12
            return $c['sources.fileSystem'];
177 41
        });
178
179 41
        $this['sources.fileSystem.cached'] = $this->factory(function(Container $c) {
180 2
            $cache = $c['finder.cache.adapter'];
181 2
            $adapter = new Cache(
0 ignored issues
show
Deprecated Code introduced by
The class Gaufrette\Adapter\Cache has been deprecated with message: The Cache adapter is deprecated since version 0.4 and will be removed in 1.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
182 2
                $c['sources.fileSystem.adapter'],
183
                $cache,
184 2
                $c['finder.cache.duration'],
185
                $cache
186
            );
187
188 2
            return new Filesystem($adapter);
189 41
        });
190 41
    }
191
192 41
    private function initializeFinder(): void
193
    {
194 41
        $this['finder.cache.path'] = self::FINDER_CACHE_DIRECTORY;
195 41
        $this['finder.cache.duration'] = self::FINDER_CACHE_DURATION;
196
197 41
        $this['finder'] = $this->factory(function(Container $c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
198 12
            return new Finder($this['sources.fileSystem.finder']);
199 41
        });
200
201 41
        $this['finder.cache.adapter'] = $this->factory(function(Container $c) {
202
            return new Local($c['finder.cache.path'], true);
203 41
        });
204 41
    }
205
206 41
    private function initializeServices(): void
207
    {
208 41
        $this['logger'] = $this->factory(function(Container $c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
209 5
            return new \Psr\Log\NullLogger();
210 41
        });
211
212 41
        $this['formatter.provider'] = function (Container $c) {
213 11
            return new ProfileProvider($c['profile']);
214
        };
215
216 41
        $this['hydrator'] = $this->factory(function(Container $c) {
217 11
            $hydrator = new Hydrator($c['sources.fileSystem'], $c['target.fileSystem'], $c['configuration'], $c['finder'], $c['formatter.provider']);
218 11
            $hydrator->allowNonDistFilesOverwrite($c['hydrator.allowNonDistFilesOverwrite']);
219
220 11
            $hydrator->setLogger($c['logger'])
221 11
                ->setSuffix($c['distFiles.suffix']);
222
223 11
            return $hydrator;
224 41
        });
225
226 41
        $this['generator.nameTranslator'] = function (Container $c) {
227 2
            $translator = new FilePrefixTranslator();
228 2
            $translator->changeMasterFile($c['configuration.masterFile']);
229
230 2
            return $translator;
231
        };
232
233 41
        $this['generator.variableProvider'] = $this->factory(function (Container $c) {
234 6
            $provider = new VariableProvider($c['parser']);
235
236 6
            $profile = $c['profile'];
237 6
            $options = $profile->getGeneratorOptions();
238 6
            if(! isset($options['translator']) || $options['translator'] === 'prefix')
239
            {
240 2
                $provider->setNameTranslator($c['generator.nameTranslator']);
241
            }
242
243 6
            return $provider;
244 41
        });
245
246 41
        $this['configurationFilesGenerator'] = function (Container $c) {
247 6
            return new YamlGenerator($c['generate.sources.fileSystem'], $c['configuration'], $c['generator.variableProvider']);
248
        };
249 41
    }
250
}
251