Completed
Push — develop ( 2993ce...48a3ec )
by Mike
09:32
created

ConfigurationFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 116
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A addMiddleware() 0 4 1
A fromDefaultLocations() 0 12 3
A registerStrategy() 0 4 1
A applyMiddleware() 0 8 2
A fromUri() 0 19 4
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\Strategy;
19
use phpDocumentor\Configuration\Factory\Version3;
20
use phpDocumentor\Uri;
21
use RuntimeException;
22
23
/**
24
 * The ConfigurationFactory converts the configuration xml from a Uri into an array.
25
 */
26
final class ConfigurationFactory
27
{
28
    /**
29
     * @var Strategy[] All strategies that are used by the ConfigurationFactory.
30
     */
31
    private $strategies = [];
32
33
    /**
34
     * A series of callables that take the configuration array as parameter and should return that array or a modified
35
     * version of it.
36
     *
37
     * @var callable[]
38
     */
39
    private $middlewares = [];
40
41
    /**
42
     * @var string[]
43
     */
44
    private $defaultFiles = [];
45
46
    /**
47
     * Initializes the ConfigurationFactory.
48
     *
49
     * @param Strategy[]|iterable $strategies
50
     * @param array $defaultFiles
51
     */
52 8
    public function __construct(iterable $strategies, array $defaultFiles)
53
    {
54 8
        foreach ($strategies as $strategy) {
55 6
            $this->registerStrategy($strategy);
56
        }
57 7
        $this->defaultFiles = $defaultFiles;
58 7
    }
59
60
    /**
61
     * Adds a middleware callback that allows the consumer to alter the configuration array when it is constructed.
62
     *
63
     * @param callable $middleware
64
     */
65 2
    public function addMiddleware(callable $middleware): void
66
    {
67 2
        $this->middlewares[] = $middleware;
68 2
    }
69
70
    /**
71
     * Attempts to load a configuration from the default locations for phpDocumentor
72
     *
73
     * @return Configuration
74
     */
75 3
    public function fromDefaultLocations(): Configuration
76
    {
77 3
        foreach ($this->defaultFiles as $file) {
78
            try {
79 2
                return $this->fromUri(new Uri($file));
80 1
            } catch (\InvalidArgumentException $e) {
81 1
                continue;
82
            }
83
        }
84
85 2
        return new Configuration($this->applyMiddleware(Version3::buildDefault()));
86
    }
87
88
    /**
89
     * Converts the phpDocumentor configuration xml to an array.
90
     *
91
     * @param Uri $uri The location of the file to be loaded.
92
     *
93
     * @return Configuration
94
     * @throws RuntimeException if no matching strategy can be found.
95
     */
96 3
    public function fromUri(Uri $uri): Configuration
97
    {
98 3
        $filename = (string) $uri;
99
100 3
        if (!file_exists($filename)) {
101 1
            throw new \InvalidArgumentException(sprintf('File %s could not be found', $filename));
102
        }
103
104 2
        $xml = new \SimpleXMLElement($filename, 0, true);
105 2
        foreach ($this->strategies as $strategy) {
106 1
            if ($strategy->supports($xml) !== true) {
107 1
                continue;
108
            }
109
110 1
            return new Configuration($this->applyMiddleware($strategy->convert($xml)));
111
        }
112
113 1
        throw new RuntimeException('No supported configuration files were found');
114
    }
115
116
    /**
117
     * Adds strategies that are used in the ConfigurationFactory.
118
     *
119
     * @param Strategy $strategy
120
     */
121 5
    private function registerStrategy(Strategy $strategy): void
122
    {
123 5
        $this->strategies[] = $strategy;
124 5
    }
125
126
    /**
127
     * Applies all middleware callbacks onto the configuration.
128
     *
129
     * @param array $configuration
130
     *
131
     * @return array
132
     */
133 5
    private function applyMiddleware(array $configuration): array
134
    {
135 5
        foreach ($this->middlewares as $middleware) {
136 2
            $configuration = $middleware($configuration);
137
        }
138
139 5
        return $configuration;
140
    }
141
}
142