Completed
Push — develop ( 521632...eb9c4a )
by Mike
06:46
created

overwriteDestinationFolder()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Configuration;
17
18
use phpDocumentor\Configuration\Factory\Version3;
19
use phpDocumentor\Dsn;
20
use phpDocumentor\Path;
21
22
final class CommandlineOptionsMiddleware
23
{
24
    /** @var array */
25
    private $options = [];
26
27 15
    public function __construct(array $options = [])
28
    {
29 15
        $this->options = $options;
30 15
    }
31
32 13
    public function __invoke(array $configuration): array
33
    {
34 13
        $configuration = $this->overwriteDestinationFolder($configuration);
35 13
        $configuration = $this->disableCache($configuration);
36 13
        $configuration = $this->overwriteCacheFolder($configuration);
37 13
        $configuration = $this->overwriteTitle($configuration);
38 13
        $configuration = $this->overwriteTemplates($configuration);
39
40 13
        if (!isset($configuration['phpdocumentor']['versions'])) {
41 5
            return $configuration;
42
        }
43
44 8
        foreach ($configuration['phpdocumentor']['versions'] as &$version) {
45 8
            $version = $this->setFilesInPath($version);
46 8
            $version = $this->setDirectoriesInPath($version);
47 8
            $version = $this->registerExtensions($version);
48 8
            $version = $this->overwriteIgnoredPaths($version);
49 8
            $version = $this->overwriteMarkers($version);
50 8
            $version = $this->overwriteIncludeSource($version);
51 8
            $version = $this->overwriteVisibility($version);
52
            $version = $this->overwriteDefaultPackageName($version);
53
        }
54 8
55
        return $configuration;
56
    }
57 15
58
    private function overwriteDestinationFolder(array $configuration): array
59 15
    {
60 1
        if (isset($this->options['target']) && $this->options['target']) {
61
            $configuration['phpdocumentor']['paths']['output'] = new Dsn($this->options['target']);
62
        }
63 15
64
        return $configuration;
65
    }
66
67
    /**
68
     * Changes the given configuration array so that the cache handling is disabled.
69 15
     */
70
    private function disableCache(array $configuration): array
71 15
    {
72 2
        if (isset($this->options['force']) && $this->options['force']) {
73
            $configuration['phpdocumentor']['use-cache'] = false;
74
        }
75 15
76
        return $configuration;
77
    }
78 15
79
    private function overwriteCacheFolder(array $configuration): array
80 15
    {
81 1
        if (isset($this->options['cache-folder']) && $this->options['cache-folder']) {
82
            $configuration['phpdocumentor']['paths']['cache'] = new Path($this->options['cache-folder']);
83
        }
84 15
85
        return $configuration;
86
    }
87 15
88
    private function overwriteTitle(array $configuration): array
89 15
    {
90 1
        if (isset($this->options['title']) && $this->options['title']) {
91
            $configuration['phpdocumentor']['title'] = $this->options['title'];
92
        }
93 15
94
        return $configuration;
95
    }
96
97
    /**
98
     * Changes the given configuration array to feature the templates from the options.
99 15
     */
100
    private function overwriteTemplates(array $configuration): array
101 15
    {
102 1
        if (isset($this->options['template']) && $this->options['template']) {
103 1
            $configuration['phpdocumentor']['templates'] = array_map(
104 1
                function ($templateName) {
105 1
                    return ['name' => $templateName];
106 1
                },
107
                (array)$this->options['template']
108
            );
109
        }
110 15
111
        return $configuration;
112
    }
113 10
114
    private function setFilesInPath(array $version): array
115 10
    {
116 9
        if (! isset($this->options['filename']) || ! $this->options['filename']) {
117
            return $version;
118
        }
119 1
120 1
        if (! isset($version['api'])) {
121
            $version['api'] = $this->createDefaultApiSettings();
122
        }
123 1
124 1
        $version['api'][0]['source']['paths'] = array_map(
125 1
            function ($path) {
126 1
                return new Path($path);
127 1
            },
128
            $this->options['filename']
129
        );
130 1
131
        return $version;
132
    }
133 10
134
    private function setDirectoriesInPath(array $version): array
135 10
    {
136 7
        if (! isset($this->options['directory']) || ! $this->options['directory']) {
137
            return $version;
138
        }
139 3
140
        $currentApiConfig = current($this->createDefaultApiSettings());
141 3
142 2
        if (isset($version['api'])) {
143
            $currentApiConfig = current($version['api']);
144
        }
145
146 3
        //Reset the current config, because directory it overwriting the config.
147 3
        $currentApiConfig['source']['paths'] = [];
148
        $version['api'] = [];
149 3
150
        foreach ($this->options['directory'] as $path) {
151
            //If the passed directory is an absolute path this should be handled as a new Api
152 3
            //A version may contain multiple APIs.
153 2
            if (Path::isAbsolutePath($path)) {
154 2
                $apiConfig = $currentApiConfig;
155 2
                $apiConfig['source']['dsn'] = new Dsn($path);
156 2
                $apiConfig['source']['paths'] = [ new Path('./')];
157
                $version['api'][] = $apiConfig;
158 3
            } else {
159
                $currentApiConfig['source']['paths'][] = new Path($path);
160
            }
161
        }
162 3
163 2
        if (count($currentApiConfig['source']['paths']) > 0) {
164
            $version['api'][] = $currentApiConfig;
165
        }
166 3
167
        return $version;
168
    }
169 10
170
    private function registerExtensions(array $version): array
171 10
    {
172 9
        if (!isset($this->options['extensions']) || ! $this->options['extensions']) {
173
            return $version;
174
        }
175 1
176 1
        if (! isset($version['api'])) {
177
            $version['api'] = $this->createDefaultApiSettings();
178
        }
179 1
180
        $version['api'][0]['extensions'] = $this->options['extensions'];
181 1
182
        return $version;
183
    }
184 10
185
    private function overwriteIgnoredPaths(array $version): array
186 10
    {
187 9
        if (! isset($this->options['ignore']) || ! $this->options['ignore']) {
188
            return $version;
189
        }
190 1
191 1
        if (! isset($version['api'])) {
192
            $version['api'] = $this->createDefaultApiSettings();
193
        }
194 1
195 1
        $version['api'][0]['ignore']['paths'] = array_map(
196 1
            function ($path) {
197 1
                return new Path($path);
198 1
            },
199
            $this->options['ignore']
200
        );
201 1
202
        return $version;
203
    }
204 10
205
    private function overwriteMarkers(array $version): array
206 10
    {
207 9
        if (! isset($this->options['markers']) || ! $this->options['markers']) {
208
            return $version;
209
        }
210 1
211 1
        if (! isset($version['api'])) {
212
            $version['api'] = $this->createDefaultApiSettings();
213
        }
214 1
215
        $version['api'][0]['markers'] = $this->options['markers'];
216 1
217
        return $version;
218
    }
219 10
220
    private function overwriteIncludeSource(array $version): array
221 10
    {
222 9
        if (! isset($this->options['sourcecode'])) {
223
            return $version;
224
        }
225 1
226 1
        if (! isset($version['api'])) {
227
            $version['api'] = $this->createDefaultApiSettings();
228
        }
229 1
230
        $version['api'][0]['include-source'] = $this->options['sourcecode'];
231 1
232
        return $version;
233
    }
234 10
235
    private function overwriteVisibility(array $version): array
236 10
    {
237 9
        if (! isset($this->options['visibility']) || ! $this->options['visibility']) {
238
            return $version;
239
        }
240 1
241 1
        if (! isset($version['api'])) {
242
            $version['api'] = $this->createDefaultApiSettings();
243
        }
244 1
245
        $version['api'][0]['visibility'] = $this->options['visibility'];
246 1
247
        return $version;
248
    }
249 9
250
    private function overwriteDefaultPackageName(array $version): array
251 9
    {
252
        if (! isset($this->options['defaultpackagename']) || ! $this->options['defaultpackagename']) {
253
            return $version;
254
        }
255
256
        if (! isset($version['api'])) {
257
            $version['api'] = $this->createDefaultApiSettings();
258
        }
259
260
        $version['api'][0]['default-package-name'] = $this->options['defaultpackagename'];
261
262
        return $version;
263
    }
264
265
    private function createDefaultApiSettings(): array
266
    {
267
        return current(Version3::buildDefault()['phpdocumentor']['versions'])['api'];
268
    }
269
}
270