Passed
Push — nln-php7 ( 12eaac )
by Nicolas
05:28
created

Application   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 94.31%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 16
dl 0
loc 213
ccs 116
cts 123
cp 0.9431
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A initializeParameters() 0 11 1
A initializeConfiguration() 0 33 1
A initializeProfile() 0 14 1
B initializeSourceFileSystem() 0 66 5
A initializeFinder() 0 13 1
A initializeServices() 0 44 3
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.0.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
    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 32
        $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 31
        $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
    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 38
        $this['profile'] = function(Container $c) {
103 38
            return new ProfileReader($c['profile.fileSystem']);
104
        };
105 41
    }
106
107
    private function initializeSourceFileSystem(): void
108
    {
109 41
        $this['sources.fileSystem.adapter'] = $this->factory(function(Container $c) {
110
111 3
            $paths = $c['sources.path'];
112
113 3
            if(! is_array($paths))
114
            {
115 3
                $paths = array($paths);
116
            }
117
118 3
            $adapter = new MultipleAdapter();
119
120 3
            foreach($paths as $path)
121
            {
122 3
                $localAdapter = new Local($path);
123
124 3
                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 3
                $adapter->mount($path, $localAdapter);
132
            }
133
134 3
            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 18
            return new Filesystem($c['sources.fileSystem.adapter']);
155 41
        });
156
157 41
        $this['sources.fileSystem.finder'] = $this->factory(function(Container $c) {
158 12
            return $c['sources.fileSystem'];
159 41
        });
160
161 41
        $this['sources.fileSystem.cached'] = $this->factory(function(Container $c) {
162 2
            $cache = $c['finder.cache.adapter'];
163 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...
164 2
                $c['sources.fileSystem.adapter'],
165 2
                $cache,
166 2
                $c['finder.cache.duration'],
167 2
                $cache
168
            );
169
170 2
            return new Filesystem($adapter);
171 41
        });
172 41
    }
173
174 41
    private function initializeFinder(): void
175
    {
176 41
        $this['finder.cache.path'] = self::FINDER_CACHE_DIRECTORY;
177 41
        $this['finder.cache.duration'] = self::FINDER_CACHE_DURATION;
178
179 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...
180 12
            return new Finder($this['sources.fileSystem.finder']);
181 41
        });
182
183 41
        $this['finder.cache.adapter'] = $this->factory(function(Container $c) {
184
            return new Local($c['finder.cache.path'], true);
185 41
        });
186 41
    }
187
188
    private function initializeServices(): void
189
    {
190 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...
191 5
            return new \Psr\Log\NullLogger();
192 41
        });
193
194 11
        $this['formatter.provider'] = function (Container $c) {
195 11
            return new ProfileProvider($c['profile']);
196
        };
197
198 41
        $this['hydrator'] = $this->factory(function(Container $c) {
199 11
            $hydrator = new Hydrator($c['sources.fileSystem'], $c['target.fileSystem'], $c['configuration'], $c['finder'], $c['formatter.provider']);
200 11
            $hydrator->allowNonDistFilesOverwrite($c['hydrator.allowNonDistFilesOverwrite']);
201
202 11
            $hydrator->setLogger($c['logger'])
203 11
                ->setSuffix($c['distFiles.suffix']);
204
205 11
            return $hydrator;
206 41
        });
207
208 2
        $this['generator.nameTranslator'] = function (Container $c) {
209 2
            $translator = new FilePrefixTranslator();
210 2
            $translator->changeMasterFile($c['configuration.masterFile']);
211
212 2
            return $translator;
213
        };
214
215 41
        $this['generator.variableProvider'] = $this->factory(function (Container $c) {
216 6
            $provider = new VariableProvider($c['parser']);
217
218 6
            $profile = $c['profile'];
219 6
            $options = $profile->getGeneratorOptions();
220 6
            if(! isset($options['translator']) || $options['translator'] === 'prefix')
221
            {
222 2
                $provider->setNameTranslator($c['generator.nameTranslator']);
223
            }
224
225 6
            return $provider;
226 41
        });
227
228 6
        $this['configurationFilesGenerator'] = function (Container $c) {
229 6
            return new YamlGenerator($c['sources.fileSystem'], $c['configuration'], $c['generator.variableProvider']);
230
        };
231
    }
232
}
233