Completed
Push — master ( aa144c...0ba72b )
by Jonathan
02:44
created

Container::initialize()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 87
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 87
ccs 2
cts 2
cp 1
rs 8.6296
c 0
b 0
f 0
cc 1
eloc 48
nc 1
nop 0
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PHPChunkit;
6
7
use PHPChunkit\Command;
8
use PHPChunkit\GenerateTestClass;
9
use Pimple\Container as PimpleContainer;
10
use Symfony\Component\Console\Application;
11
12
class Container extends PimpleContainer
13
{
14
    const NAME = 'PHPChunkit';
15
    const VERSION = '0.0.1';
16
17 1
    public function initialize()
18
    {
19
        $this['phpchunkit.configuration'] = function() {
20
            return $this->getConfiguration();
21
        };
22
23
        $this['phpchunkit.symfony_application'] = function() {
24
            return new Application(self::NAME, self::VERSION);
25
        };
26
27
        $this['phpchunkit.application'] = function() {
28
            return new PHPChunkitApplication($this);
29
        };
30
31
        $this['phpchunkit.database_sandbox'] = function() {
32
            return $this['phpchunkit.configuration']->getDatabaseSandbox();
33
        };
34
35
        $this['phpchunkit.event_dispatcher'] = function() {
36
            return $this['phpchunkit.configuration']->getEventDispatcher();
37
        };
38
39
        $this['phpchunkit.test_chunker'] = function() {
40
            return new TestChunker($this['phpchunkit.test_counter']);
41
        };
42
43
        $this['phpchunkit.test_runner'] = function() {
44
            return new TestRunner(
45
                $this['phpchunkit.symfony_application'],
46
                $this['phpchunkit.application.input'],
47
                $this['phpchunkit.application.output'],
48
                $this['phpchunkit.configuration']
49
            );
50
        };
51
52
        $this['phpchunkit.test_counter'] = function() {
53
            return new TestCounter(
54
                $this['phpchunkit.file_classes_helper']
55
            );
56
        };
57
58
        $this['phpchunkit.test_finder'] = function() {
59
            return new TestFinder(
60
                $this['phpchunkit.configuration']->getTestsDirectory()
61
            );
62
        };
63
64
        $this['phpchunkit.command.setup'] = function() {
65
            return new Command\Setup();
66
        };
67
68
        $this['phpchunkit.command.test_watcher'] = function() {
69
            return new Command\TestWatcher(
70
                $this['phpchunkit.test_runner'],
71
                $this['phpchunkit.configuration'],
72
                $this['phpchunkit.file_classes_helper']
73
            );
74
        };
75
76
        $this['phpchunkit.command.run'] = function() {
77
            return new Command\Run(
78
                $this['phpchunkit.test_runner'],
79
                $this['phpchunkit.configuration'],
80
                $this['phpchunkit.test_chunker'],
81
                $this['phpchunkit.test_finder']
82
            );
83
        };
84
85
        $this['phpchunkit.command.create_databases'] = function() {
86
            return new Command\CreateDatabases($this['phpchunkit.event_dispatcher']);
87
        };
88
89
        $this['phpchunkit.command.build_sandbox'] = function() {
90
            return new Command\BuildSandbox(
91
                $this['phpchunkit.test_runner'],
92
                $this['phpchunkit.event_dispatcher']
93
            );
94
        };
95
96
        $this['phpchunkit.command.generate_test'] = function() {
97
            return new Command\Generate(new GenerateTestClass());
98
        };
99
100
        $this['phpchunkit.file_classes_helper'] = function() {
101
            return new FileClassesHelper();
102
        };
103 1
    }
104
105
    private function getConfiguration() : Configuration
106
    {
107
        $configuration = $this->loadConfiguration();
108
109
        $this->loadPHPChunkitBootstrap($configuration);
110
111
        // try to guess watch directories
112
        if (!$configuration->getWatchDirectories()) {
113
            $paths = [
114
                sprintf('%s/src', $configuration->getRootDir()),
115
                sprintf('%s/lib', $configuration->getRootDir()),
116
                sprintf('%s/tests', $configuration->getRootDir()),
117
            ];
118
119
            $watchDirectories = [];
120
            foreach ($paths as $path) {
121
                if (is_dir($path)) {
122
                    $watchDirectories[] = $path;
123
                }
124
            }
125
126
            $configuration->setWatchDirectories($watchDirectories);
127
        }
128
129
        // try to guess tests directory
130
        if (!$configuration->getTestsDirectory()) {
131
            $paths = [
132
                sprintf('%s/tests', $configuration->getRootDir()),
133
                sprintf('%s/src', $configuration->getRootDir()),
134
                sprintf('%s/lib', $configuration->getRootDir()),
135
            ];
136
137
            foreach ($paths as $path) {
138
                if (is_dir($path)) {
139
                    $configuration->setTestsDirectory($path);
140
                    break;
141
                }
142
            }
143
        }
144
145
        return $configuration;
146
    }
147
148
    private function loadConfiguration() : Configuration
149
    {
150
        $xmlPath = $this->findPHPChunkitXmlPath();
151
152
        $configuration = $xmlPath
153
            ? Configuration::createFromXmlFile($xmlPath)
154
            : new Configuration()
155
        ;
156
157
        $configuration->setSandboxEnabled($this->isSandboxEnabled());
158
159
        if (!$configuration->getRootDir()) {
160
            $configuration->setRootDir($this['phpchunkit.root_dir']);
161
        }
162
163
        return $configuration;
164
    }
165
166
    private function isSandboxEnabled() : bool
167
    {
168
        return array_filter($_SERVER['argv'], function($arg) {
169
            return strpos($arg, 'sandbox') !== false;
170
        }) ? true : false;
171
    }
172
173
    /**
174
     * @return null|string
175
     */
176
    private function findPHPChunkitXmlPath()
177
    {
178
        if (file_exists($distXmlPath = $this['phpchunkit.root_dir'].'/phpchunkit.xml.dist')) {
179
            return $distXmlPath;
180
        }
181
182
        if (file_exists($defaultXmlPath = $this['phpchunkit.root_dir'].'/phpchunkit.xml')) {
183
            return $defaultXmlPath;
184
        }
185
    }
186
187
    /**
188
     * @throws InvalidArgumentException
189
     */
190
    private function loadPHPChunkitBootstrap(Configuration $configuration)
191
    {
192
        if ($bootstrapPath = $configuration->getBootstrapPath()) {
193
            if (!file_exists($bootstrapPath)) {
194
                throw new \InvalidArgumentException(
195
                    sprintf('Bootstrap path "%s" does not exist.', $bootstrapPath)
196
                );
197
            }
198
199
            require_once $bootstrapPath;
200
        } else {
201
            $autoloaderPath = sprintf('%s/vendor/autoload.php', $configuration->getRootDir());
202
203
            if (file_exists($autoloaderPath)) {
204
                require_once $autoloaderPath;
205
            }
206
        }
207
    }
208
}
209