ConfigurationFactory   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

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

6 Methods

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