Wiring::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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-2017 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-2017 Michael Cummings
32
 * @license   LGPL-3.0+
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal\Configuration;
36
37
use Yapeal\Container\ContainerInterface;
38
39
/**
40
 * Class Wiring
41
 */
42
class Wiring
43
{
44
    /**
45
     * @param ContainerInterface $dic
46
     */
47 1
    public function __construct(ContainerInterface $dic)
48
    {
49 1
        $this->dic = $dic;
50
    }
51
    /**
52
     * This is used to configure/wire all the pieces of Yapeal-ng together.
53
     *
54
     * It is not required to use this method or even this class but the rest of Yapeal-ng relies heavily on the
55
     * container $dic having been setup in a way that mimics it closely. With all the other ways this method and other
56
     * parts of Yapeal-ng provides to override or change things it is __very strongly__ suggested that application
57
     * developers use it.
58
     *
59
     * _NOTE:_
60
     *
61
     *     The Yapeal.Wiring.Classes.config setting has extra special handling because everything else is very heavily
62
     *     dependant on the initial paths it determines which are used to find everything else. An application developer
63
     *     considering override this setting should shoot yourself in the foot first to help dull the pain you will be
64
     *     causing yourself by overriding it. For the masochists that go ahead, enjoy it, but I don't want any pictures
65
     *     or descriptive e-mail sent to me about your experience please.
66
     *
67
     * @return self Fluent interface.
68
     * @throws \LogicException
69
     */
70
    public function wireAll(): self
71
    {
72
        $dic = $this->dic;
73
        // First things first, should add self to Container and freeze so can't be overwritten later by oops.
74
        if (empty($dic['Yapeal.Wiring.Callable.Wiring'])) {
75
            /**
76
             * @param ContainerInterface $dic
77
             *
78
             * @return Wiring
79
             */
80
            $dic['Yapeal.Wiring.Callable.Wiring'] = function (ContainerInterface $dic) {
81
                return new Wiring($dic);
82
            };
83
            $dic['Yapeal.Wiring.Callable.Wiring'];
84
        }
85
        $base = 'Yapeal.Wiring.Classes.';
86
        $dic[$base . 'config'] = $dic[$base . 'config'] ?? '\Yapeal\Configuration\ConfigWiring';
87
        $names = ['Config', 'Event', 'Log', 'Sql', 'Xml', 'Xsd', 'Xsl', 'FileSystem', 'Network', 'EveApi'];
88
        /**
89
         * @var WiringInterface $class
90
         */
91
        foreach ($names as $name) {
92
            $setting = $base . strtolower($name);
93
            if (!empty($dic[$setting])
94
                && is_subclass_of($dic[$setting], WiringInterface::class, true)
95
            ) {
96
                $class = new $dic[$setting];
97
                $class->wire($dic);
98
                continue;
99
            }
100
            $mess = sprintf('Could NOT find mandatory %s wiring class. Aborting ...', $name);
101
            throw new \LogicException($mess);
102
        }
103
        return $this;
104
    }
105
    /**
106
     * @var ContainerInterface $dic
107
     */
108
    private $dic;
109
}
110