XsdWiring   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 5
dl 0
loc 81
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A wire() 0 9 1
B wireCreator() 0 34 3
A wireValidator() 0 17 2
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains class XsdWiring.
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) 2016-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 2016-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
use Yapeal\Event\MediatorInterface;
39
use Yapeal\Xsd\ValidatorInterface;
40
41
/**
42
 * Class XsdWiring.
43
 */
44
class XsdWiring implements WiringInterface
45
{
46
    /**
47
     * @param ContainerInterface $dic
48
     *
49
     * @throws \LogicException
50
     * @throws \InvalidArgumentException
51
     */
52
    public function wire(ContainerInterface $dic)
53
    {
54
        /**
55
         * @var MediatorInterface $mediator
56
         */
57
        $mediator = $dic['Yapeal.Event.Callable.Mediator'];
58
        $this->wireCreator($dic, $mediator)
59
            ->wireValidator($dic, $mediator);
60
    }
61
    /**
62
     * @param ContainerInterface $dic
63
     * @param MediatorInterface  $mediator
64
     *
65
     * @return XsdWiring Fluent interface.
66
     */
67
    private function wireCreator(ContainerInterface $dic, MediatorInterface $mediator): self
68
    {
69
        if (empty($dic['Yapeal.Xsd.Callable.Creator'])) {
70
            /**
71
             * @param ContainerInterface $dic
72
             *
73
             * @return \Yapeal\Xsd\Creator
74
             * @throws \LogicException
75
             */
76
            $dic['Yapeal.Xsd.Callable.Creator'] = function (ContainerInterface $dic) {
77
                $loader = new \Twig_Loader_Filesystem($dic['Yapeal.Xsd.dir']);
78
                $twig = new \Twig_Environment($loader,
79
                    ['debug' => true, 'strict_variables' => true, 'autoescape' => false]);
80
                $filter = new \Twig_SimpleFilter('ucFirst', function ($value) {
81
                    return ucfirst($value);
82
                });
83
                $twig->addFilter($filter);
84
                $filter = new \Twig_SimpleFilter('lcFirst', function ($value) {
85
                    return lcfirst($value);
86
                });
87
                $twig->addFilter($filter);
88
                /**
89
                 * @var \Yapeal\Xsd\Creator $create
90
                 */
91
                $create = new $dic['Yapeal.Xsd.Classes.create']($twig, $dic['Yapeal.Xsd.dir']);
92
                if (!empty($dic['Yapeal.Create.overwrite'])) {
93
                    $create->setOverwrite($dic['Yapeal.Create.overwrite']);
94
                }
95
                return $create;
96
            };
97
            $mediator->addServiceListener('Yapeal.EveApi.create', ['Yapeal.Xsd.Callable.Creator', 'createXsd'], 'last');
98
        }
99
        return $this;
100
    }
101
    /**
102
     * @param ContainerInterface $dic
103
     * @param MediatorInterface  $mediator
104
     *
105
     * @return self Fluent interface.
106
     */
107
    private function wireValidator(ContainerInterface $dic, MediatorInterface $mediator): self
108
    {
109
        if (empty($dic['Yapeal.Xsd.Callable.Validator'])) {
110
            /**
111
             * @param ContainerInterface $dic
112
             *
113
             * @return ValidatorInterface
114
             */
115
            $dic['Yapeal.Xsd.Callable.Validator'] = function (ContainerInterface $dic): ValidatorInterface {
116
                return new $dic['Yapeal.Xsd.Classes.validate']($dic['Yapeal.Xsd.dir']);
117
            };
118
            $mediator->addServiceListener('Yapeal.EveApi.validate',
119
                ['Yapeal.Xsd.Callable.Validator', 'validateEveApi'],
120
                'last');
121
        }
122
        return $this;
123
    }
124
}
125