Completed
Push — master ( 31b5db...524ecf )
by Michael
03:26
created

EveApiWiring   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 8
dl 0
loc 118
ccs 0
cts 82
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C wire() 0 43 7
B getFilteredEveApiSubscriberList() 0 30 5
B wireCreator() 0 27 3
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains class EveApiWiring.
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 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 Michael Cummings
32
 * @license   LGPL-3.0+
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal\Configuration;
36
37
use FilePathNormalizer\FilePathNormalizerTrait;
38
use Yapeal\Container\ContainerInterface;
39
use Yapeal\Event\MediatorInterface;
40
41
/**
42
 * Class EveApiWiring.
43
 */
44
class EveApiWiring implements WiringInterface
45
{
46
    use FilePathNormalizerTrait;
47
    /**
48
     * @param ContainerInterface $dic
49
     *
50
     * @throws \LogicException
51
     */
52
    public function wire(ContainerInterface $dic)
53
    {
54
        if (empty($dic['Yapeal.Event.Mediator'])) {
55
            $mess = 'Tried to call Mediator before it has been added';
56
            throw new \LogicException($mess);
57
        }
58
        /**
59
         * @var MediatorInterface $mediator
60
         */
61
        $mediator = $dic['Yapeal.Event.Mediator'];
62
        $internal = $this->getFilteredEveApiSubscriberList($dic);
63
        if (0 !== count($internal)) {
64
            foreach ($internal as $listener) {
65
                $service = sprintf('%1$s.%2$s.%3$s',
66
                    'Yapeal.EveApi',
67
                    basename(dirname($listener)),
68
                    basename($listener, '.php'));
69
                if (empty($dic[$service])) {
70
                    $dic[$service] = function () use ($dic, $service) {
71
                        $class = '\\' . str_replace('.', '\\', $service);
72
                        /**
73
                         * @var \Yapeal\CommonToolsInterface $callable
74
                         */
75
                        $callable = new $class();
76
                        $callable->setCsq($dic['Yapeal.Sql.CommonQueries'])
77
                            ->setPdo($dic['Yapeal.Sql.Connection']);
78
                        if (false === strpos($service, 'Section')) {
79
                            /**
80
                             * @var \Yapeal\Event\EveApiPreserverInterface $callable
81
                             */
82
                            $callable->setPreserve($dic['Yapeal.EveApi.Cache.preserve']);
83
                        }
84
                        return $callable;
85
                    };
86
                }
87
                $mediator->addServiceListener($service . '.start', [$service, 'startEveApi'], 'last');
88
                if (false === strpos($listener, 'Section')) {
89
                    $mediator->addServiceListener($service . '.preserve', [$service, 'preserveEveApi'], 'last');
90
                }
91
            }
92
        }
93
        $this->wireCreator($dic, $mediator);
94
    }
95
    /**
96
     * @param ContainerInterface $dic
97
     *
98
     * @return array
99
     */
100
    private function getFilteredEveApiSubscriberList(ContainerInterface $dic): array
101
    {
102
        $flags = \FilesystemIterator::CURRENT_AS_FILEINFO
103
            | \FilesystemIterator::KEY_AS_PATHNAME
104
            | \FilesystemIterator::SKIP_DOTS
105
            | \FilesystemIterator::UNIX_PATHS;
106
        $rdi = new \RecursiveDirectoryIterator($dic['Yapeal.EveApi.dir']);
107
        $rdi->setFlags($flags);
108
        /** @noinspection SpellCheckingInspection */
109
        $rcfi = new \RecursiveCallbackFilterIterator($rdi,
110
            function (\SplFileInfo $current, $key, \RecursiveDirectoryIterator $rdi) {
111
                if ($rdi->hasChildren()) {
112
                    return true;
113
                }
114
                $dirs = ['Account', 'Api', 'Char', 'Corp', 'Eve', 'Map', 'Server'];
115
                $dirExists = in_array(basename(dirname($key)), $dirs, true);
116
                return ($dirExists && $current->isFile() && 'php' === $current->getExtension());
117
            });
118
        /** @noinspection SpellCheckingInspection */
119
        $rii = new \RecursiveIteratorIterator($rcfi,
120
            \RecursiveIteratorIterator::LEAVES_ONLY,
121
            \RecursiveIteratorIterator::CATCH_GET_CHILD);
122
        $rii->setMaxDepth(3);
123
        $fpn = $this->getFpn();
124
        $files = [];
125
        foreach ($rii as $file) {
126
            $files[] = $fpn->normalizeFile($file->getPathname());
127
        }
128
        return $files;
129
    }
130
    /**
131
     * @param ContainerInterface $dic
132
     * @param MediatorInterface  $mediator
133
     */
134
    private function wireCreator(ContainerInterface $dic, MediatorInterface $mediator)
135
    {
136
        if (empty($dic['Yapeal.EveApi.Creator'])) {
137
            $dic['Yapeal.EveApi.Creator'] = function () use ($dic) {
138
                $loader = new \Twig_Loader_Filesystem($dic['Yapeal.EveApi.dir']);
139
                $twig = new \Twig_Environment($loader,
140
                    ['debug' => true, 'strict_variables' => true, 'autoescape' => false]);
141
                $filter = new \Twig_SimpleFilter('ucFirst', function ($value) {
142
                    return ucfirst($value);
143
                });
144
                $twig->addFilter($filter);
145
                $filter = new \Twig_SimpleFilter('lcFirst', function ($value) {
146
                    return lcfirst($value);
147
                });
148
                $twig->addFilter($filter);
149
                /**
150
                 * @var \Yapeal\EveApi\Creator $create
151
                 */
152
                $create = new $dic['Yapeal.EveApi.Handlers.create']($twig, $dic['Yapeal.EveApi.dir']);
153
                if (!empty($dic['Yapeal.Create.overwrite'])) {
154
                    $create->setOverwrite($dic['Yapeal.Create.overwrite']);
155
                }
156
                return $create;
157
            };
158
            $mediator->addServiceListener('Yapeal.EveApi.create', ['Yapeal.EveApi.Creator', 'createEveApi'], 'last');
159
        }
160
    }
161
}
162