Completed
Push — master ( f97949...b57571 )
by Michael
02:55
created

Wiring   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 4.88%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 83
ccs 2
cts 41
cp 0.0488
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B wireAll() 0 27 5
B wireConfig() 0 35 6
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains Wiring class.
5
 *
6
 * PHP version 7.0+
7
 *
8
 * LICENSE:
9
 * This file is part of Yet Another Php Eve Api Library also know as Yapeal
10
 * which can be used to access the Eve Online API data and place it into a
11
 * database.
12
 * Copyright (C) 2014-2016 Michael Cummings
13
 *
14
 * This program is free software: you can redistribute it and/or modify it
15
 * under the terms of the GNU Lesser General Public License as published by the
16
 * Free Software Foundation, either version 3 of the License, or (at your
17
 * option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful, but WITHOUT
20
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
22
 * for more details.
23
 *
24
 * You should have received a copy of the GNU Lesser General Public License
25
 * along with this program. If not, see
26
 * <http://spdx.org/licenses/LGPL-3.0.html>.
27
 *
28
 * You should be able to find a copy of this license in the COPYING-LESSER.md
29
 * file. A copy of the GNU GPL should also be available in the COPYING.md file.
30
 *
31
 * @copyright 2014-2016 Michael Cummings
32
 * @license   http://www.gnu.org/copyleft/lesser.html GNU LGPL
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal\Configuration;
36
37
use Yapeal\Cli\Yapeal\YamlConfigFile;
38
use Yapeal\Container\ContainerInterface;
39
use Yapeal\DicAwareInterface;
40
use Yapeal\DicAwareTrait;
41
42
/**
43
 * Class Wiring
44
 */
45
class Wiring implements DicAwareInterface
46
{
47
    use ConfigFileProcessingTrait, DicAwareTrait;
48
    /**
49
     * @param ContainerInterface $dic
50
     */
51 1
    public function __construct(ContainerInterface $dic)
52
    {
53 1
        $this->setDic($dic);
54
    }
55
    /**
56
     * @return self Fluent interface.
57
     * @throws \LogicException
58
     */
59
    public function wireAll()
60
    {
61
        $dic = $this->dic;
62
        $base = 'Yapeal.Wiring.Handlers.';
63
        $names = ['Config', 'Error', 'Event', 'Log', 'Sql', 'Xml', 'Xsd', 'Xsl', 'FileSystem', 'Network', 'EveApi'];
64
        /**
65
         * @var WiringInterface $class
66
         */
67
        foreach ($names as $name) {
68
            $setting = $base . strtolower($name);
69
            if (!empty($dic[$setting])
70
                && is_subclass_of($dic[$setting], '\\Yapeal\\Configuration\\WiringInterface', true)
71
            ) {
72
                $class = new $dic[$setting];
73
                $class->wire($dic);
74
                continue;
75
            }
76
            $methodName = 'wire' . $name;
77
            if (method_exists($this, $methodName)) {
78
                $this->$methodName();
79
            } else {
80
                $mess = 'Could NOT find class or method for ' . $name;
81
                throw new \LogicException($mess);
82
            }
83
        }
84
        return $this;
85
    }
86
    /**
87
     * @return void
88
     * @throws \DomainException
89
     * @throws \InvalidArgumentException
90
     * @throws \LogicException
91
     */
92
    protected function wireConfig()
93
    {
94
        $dic = $this->getDic();
95
        $path = str_replace('\\', '/', dirname(dirname(__DIR__))) . '/';
96
        // These two paths are critical to Yapeal-ng working and can't be overridden here.
97
        $dic['Yapeal.baseDir'] = $path;
98
        $dic['Yapeal.libDir'] = $path . 'lib/';
99
        if (empty($dic['Yapeal.Config.Yaml'])) {
100
            $dic['Yapeal.Config.Yaml'] = $dic->factory(function () use($dic) {
101
                return new YamlConfigFile();
102
            });
103
        }
104
        $configFiles = [
105
            __DIR__ . '/yapeal_defaults.yaml',
106
            $path . 'config/yapeal.yaml'
107
        ];
108
        $vendorPos = strpos($path, 'vendor/');
109
        if (false !== $vendorPos) {
110
            $dic['Yapeal.vendorParentDir'] = substr($path, 0, $vendorPos);
111
            $configFiles[] = $dic['Yapeal.vendorParentDir'] . 'config/yapeal.yaml';
112
        }
113
        $settings = [];
114
        // Process each file in turn so any substitutions are done in a more
115
        // consistent way.
116
        foreach ($configFiles as $configFile) {
117
            $settings = $this->parserConfigFile($configFile, $settings);
118
        }
119
        $settings = $this->doSubstitutions($settings, $dic);
0 ignored issues
show
Compatibility introduced by
$dic of type object<ArrayAccess> is not a sub-type of object<Yapeal\Container\ContainerInterface>. It seems like you assume a child interface of the interface ArrayAccess to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
120
        if (0 !== count($settings)) {
121
            // Assure NOT overwriting already existing settings given by application.
122
            foreach ($settings as $key => $value) {
123
                $dic[$key] = $dic[$key] ?? $value;
124
            }
125
        }
126
    }
127
}
128