Completed
Push — master ( 11abf2...a9443a )
by Jonathan
02:46
created

Container::isSandboxEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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