Creator::createEveApi()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 56
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 56
ccs 0
cts 54
cp 0
rs 7.7489
cc 7
eloc 46
nc 8
nop 3
crap 56

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains Creator 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) 2015-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 2015-2017 Michael Cummings
32
 * @license   LGPL-3.0+
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal\EveApi;
36
37
use Yapeal\Console\Developer\EveApi\EveApiCreatorTrait;
38
use Yapeal\Event\EveApiEventInterface;
39
use Yapeal\Event\MediatorInterface;
40
use Yapeal\FileSystem\RelativeFileSearchTrait;
41
use Yapeal\Log\Logger;
42
43
/**
44
 * Class Creator
45
 */
46
class Creator
47
{
48
    use EveApiCreatorTrait, RelativeFileSearchTrait;
49
    /**
50
     * Creator constructor.
51
     *
52
     * @param \Twig_Environment $twig
53
     * @param string            $dir
54
     */
55
    public function __construct(\Twig_Environment $twig, string $dir = __DIR__)
56
    {
57
        $this->setRelativeBaseDir($dir);
58
        $this->setTwig($twig);
59
        $this->twigExtension = 'php.twig';
60
    }
61
    /**
62
     * @param EveApiEventInterface $event
63
     * @param string               $eventName
64
     * @param MediatorInterface    $yem
65
     *
66
     * @return EveApiEventInterface
67
     * @throws \DomainException
68
     * @throws \InvalidArgumentException
69
     * @throws \LogicException
70
     */
71
    public function createEveApi(
72
        EveApiEventInterface $event,
73
        string $eventName,
74
        MediatorInterface $yem
75
    ): EveApiEventInterface
76
    {
77
        $this->setYem($yem);
78
        $data = $event->getData();
79
        $yem->triggerLogEvent('Yapeal.Log.log',
80
            Logger::DEBUG,
81
            $this->getReceivedEventMessage($data, $eventName, __CLASS__));
82
        // Only work with raw unaltered XML data.
83
        if (false !== strpos($data->getEveApiXml(), '<?yapeal.parameters.json')) {
84
            return $event->setHandledSufficiently();
85
        }
86
        $this->sectionName = $data->getEveApiSectionName();
87
        $this->apiName = $data->getEveApiName();
88
        $outputFile = sprintf('%1$s%2$s/%3$s.php',
89
            $this->getRelativeBaseDir(),
90
            ucfirst($this->sectionName),
91
            ucfirst($this->apiName));
92
        // Nothing to do if NOT overwriting and file exists.
93
        if (false === $this->isOverwrite() && is_file($outputFile)) {
94
            return $event;
95
        }
96
        $sxi = new \SimpleXMLIterator($data->getEveApiXml());
97
        $this->tables = [];
98
        $this->processValueOnly($sxi, $this->apiName);
99
        $this->processRowset($sxi, $this->apiName);
100
        $tCount = count($this->tables);
101
        if (0 === $tCount) {
102
            $mess = 'No SQL tables to create for';
103
            $yem->triggerLogEvent('Yapeal.Log.log', Logger::NOTICE, $this->createEveApiMessage($mess, $data));
104
        }
105
        ksort($this->tables);
106
        $context = [
107
            'className' => ucfirst($this->apiName),
108
            'tables' => $this->tables,
109
            'hasOwner' => $this->hasOwner(),
110
            'mask' => $data->getEveApiArgument('mask'),
111
            'namespace' => $this->getNamespace(),
112
            'sectionName' => $this->sectionName,
113
            'tableNames' => array_keys($this->tables)
114
        ];
115
        $contents = $this->getContentsFromTwig($eventName, $data, $context);
116
        if (false === $contents) {
117
            return $event;
118
        }
119
        if (false === $this->safeFileWrite($outputFile, $contents)) {
120
            $yem->triggerLogEvent($eventName,
121
                Logger::WARNING,
122
                $this->getFailedToWriteFileMessage($data, $outputFile));
123
            return $event;
124
        }
125
        return $event->setHandledSufficiently();
126
    }
127
    /**
128
     * @param \SimpleXMLIterator $sxi
129
     * @param string             $apiName
130
     * @param string             $xPath
131
     */
132
    protected function processRowset(\SimpleXMLIterator $sxi, $apiName, $xPath = '//result/rowset')
133
    {
134
        $items = $sxi->xpath($xPath);
135
        if (0 === count($items)) {
136
            return;
137
        }
138
        foreach ($items as $ele) {
139
            $rsName = ucfirst((string)$ele['name']);
140
            $colNames = explode(',', (string)$ele['columns']);
141
            $keyNames = explode(',', (string)$ele['key']);
142
            $attributes = [];
143
            foreach ($keyNames as $keyName) {
144
                $attributes[$keyName] = $this->inferDefaultFromName($keyName);
145
            }
146
            foreach ($colNames as $colName) {
147
                $attributes[$colName] = $this->inferDefaultFromName($colName);
148
            }
149
            if ($this->hasOwner()) {
150
                $attributes['ownerID'] = '$ownerID';
151
            }
152
            uksort($attributes,
153
                function ($alpha, $beta) {
154
                    return strtolower($alpha) <=> strtolower($beta);
155
                });
156
            if (0 === count($this->tables)) {
157
                $this->tables[$apiName] = ['attributes' => $attributes, 'xpath' => $rsName];
158
            } else {
159
                $this->tables[$rsName] = ['attributes' => $attributes, 'xpath' => $rsName];
160
            }
161
        }
162
    }
163
    /**
164
     * @param \SimpleXMLIterator $sxi
165
     * @param string             $tableName
166
     * @param string             $xpath
167
     */
168
    protected function processValueOnly(
169
        \SimpleXMLIterator $sxi,
170
        string $tableName,
171
        string $xpath = '//result/child::*[not(*|@*|self::dataTime)]'
172
    ) {
173
        $items = $sxi->xpath($xpath);
174
        if (false === $items || 0 === count($items)) {
175
            return;
176
        }
177
        /**
178
         * @var \SimpleXMLElement[] $items
179
         */
180
        $values = [];
181
        foreach ($items as $ele) {
182
            $name = (string)$ele->getName();
183
            $values[$name] = $this->inferDefaultFromName($name);
184
        }
185
        if ($this->hasOwner()) {
186
            $values['ownerID'] = '$ownerID';
187
        }
188
        uksort($values,
189
            function ($alpha, $beta) {
190
                return strtolower($alpha) <=> strtolower($beta);
191
            });
192
        $this->tables[$tableName] = ['values' => $values];
193
    }
194
    /**
195
     * @return string
196
     */
197
    private function getNamespace(): string
198
    {
199
        return 'Yapeal\EveApi\\' . ucfirst($this->sectionName);
200
    }
201
    /**
202
     * Used to determine if API is in section that has an owner.
203
     *
204
     * @return bool
205
     */
206
    private function hasOwner(): bool
207
    {
208
        return in_array(strtolower($this->sectionName), ['account', 'char', 'corp'], true);
209
    }
210
    /**
211
     * Used to infer(choose) default value from element or attribute's name.
212
     *
213
     * @param string $name Name of the element or attribute.
214
     *
215
     * @return string Returns the inferred value from the name.
216
     */
217
    private function inferDefaultFromName(string $name): string
218
    {
219
        $name = strtolower($name);
220
        $column = 'null';
221
        foreach ([
222
                     'descr' => '\'\'',
223
                     'name' => '\'\'',
224
                     'balance' => '\'0.0\'',
225
                     'isk' => '\'0.0\'',
226
                     'tax' => '\'0.0\'',
227
                     'timeefficiency' => 'null',
228
                     'date' => '\'1970-01-01 00:00:01\'',
229
                     'time' => '\'1970-01-01 00:00:01\'',
230
                     'until' => '\'1970-01-01 00:00:01\''
231
                 ] as $search => $replace) {
232
            if (false !== strpos($name, $search)) {
233
                return $replace;
234
            }
235
        }
236
        return $column;
237
    }
238
    /**
239
     * @var string $apiName
240
     */
241
    private $apiName;
242
    /**
243
     * @var string $sectionName
244
     */
245
    private $sectionName;
246
    /**
247
     * @var array $tables
248
     */
249
    private $tables;
250
}
251