Completed
Push — master ( b659c5...c65770 )
by Michael
10:22
created

EveApiCreator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 6
dl 0
loc 143
ccs 0
cts 85
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B createEveApi() 0 31 3
B configure() 0 30 1
A execute() 0 12 2
A processPost() 0 16 4
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains EveApiCreator 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\Console\Developer\EveApi;
36
37
use Symfony\Component\Console\Command\Command;
38
use Symfony\Component\Console\Input\InputArgument;
39
use Symfony\Component\Console\Input\InputInterface;
40
use Symfony\Component\Console\Input\InputOption;
41
use Symfony\Component\Console\Output\OutputInterface;
42
use Yapeal\Console\ConfigFileTrait;
43
use Yapeal\CommonToolsTrait;
44
use Yapeal\Container\ContainerInterface;
45
use Yapeal\Event\EveApiEventEmitterTrait;
46
use Yapeal\Event\YEMAwareInterface;
47
use Yapeal\Xml\EveApiReadWriteInterface;
48
49
/**
50
 * Class EveApiCreator
51
 */
52
class EveApiCreator extends Command implements YEMAwareInterface
53
{
54
    use CommonToolsTrait, ConfigFileTrait, EveApiEventEmitterTrait;
55
    /**
56
     * @param string             $name
57
     * @param ContainerInterface $dic
58
     *
59
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
60
     * @throws \Symfony\Component\Console\Exception\LogicException
61
     */
62
    public function __construct(string $name, ContainerInterface $dic)
63
    {
64
        $desc = 'Retrieves Eve Api XML from CCP servers and creates database class, XSD, and SQL files based on the XML'
65
            . ' structure received';
66
        $this->setDescription($desc);
67
        $this->setName($name);
68
        $this->setDic($dic);
69
        $this->setYem($dic['Yapeal.Event.Callable.Mediator']);
70
        parent::__construct($name);
71
    }
72
    /**
73
     * @param string $apiName
74
     * @param string $sectionName
75
     * @param array  $posts
76
     *
77
     * @return int
78
     * @throws \LogicException
79
     *
80
     */
81
    public function createEveApi(string $apiName, string $sectionName, array $posts): int
82
    {
83
        /**
84
         * Get new Data instance from factory.
85
         *
86
         * @var EveApiReadWriteInterface $data
87
         */
88
        $data = $this->getDic()['Yapeal.Xml.Callable.Data'];
89
        $data->setEveApiName($apiName)
90
            ->setEveApiSectionName($sectionName)
91
            ->setEveApiArguments($posts);
92
        /*
93
         * Create can't use pre-processed Eve Api XML, it needs to be unaltered version directly from the servers.
94
         * For now use Raw preserve since DB tables are not added automatically which would cause normal preservers to
95
         * fail and add junk to the logs.
96
         * NOTE: Need to decide if implementing a hybrid SQL update/init system is worth doing.
97
         */
98
        $events = [
99
            'retrieve' => 'Yapeal.EveApi.Raw',
100
            'create' => 'Yapeal.EveApi',
101
            'transform' => 'Yapeal.EveApi',
102
            'validate' => 'Yapeal.EveApi',
103
            'preserve' => 'Yapeal.EveApi.Raw'
104
        ];
105
        foreach ($events as $eventName => $eventPrefix) {
106
            if (false === $this->emitEvents($data, $eventName, $eventPrefix)) {
107
                return 2;
108
            }
109
        }
110
        return 0;
111
    }
112
    /**
113
     * Configures the current command.
114
     */
115
    protected function configure()
116
    {
117
        $help = <<<'EOF'
118
The <info>%command.name%</info> command is  used by Yapeal--ng developers to retrieve
119
the XML data from the Eve Api server and creates Yapeal Eve API Database class,
120
xsd, and sql files for most API types. Application developers will not
121
generally find it useful.
122
123
    <info>php bin/yc %command.name% section_name api_name mask [<post>]...</info>
124
125
EXAMPLES:
126
Create Char/AccountBalance class, xsd, and sql files in their respective
127
lib/{EveApi, Xsd, Sql}/Char/ directories.
128
    <info>bin/yc %command.name% char AccountBalance 1 "keyID=1156" "vCode=abc123"</info>
129
130
EOF;
131
        $this->addConfigFileOption();
132
        $this->addArgument('section_name', InputArgument::REQUIRED, 'Name of Eve Api section to retrieve.')
133
            ->addArgument('api_name', InputArgument::REQUIRED, 'Name of Eve Api to retrieve.')
134
            ->addArgument('mask', InputArgument::REQUIRED, 'Bit mask for Eve Api.')
135
            ->addArgument('post',
136
                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
137
                'Optional list of additional POST parameter(s) to send to server.',
138
                [])
139
            ->addOption('overwrite',
140
                null,
141
                InputOption::VALUE_NONE,
142
                'Causes command to overwrite any existing per Eve API files.')
143
            ->setHelp($help);
144
    }
145
    /** @noinspection PhpMissingParentCallCommonInspection */
146
    /**
147
     * Executes the current command.
148
     *
149
     * @param InputInterface  $input  An InputInterface instance
150
     * @param OutputInterface $output An OutputInterface instance
151
     *
152
     * @return int null or 0 if everything went fine, or an error code
153
     * @throws \DomainException
154
     * @throws \LogicException
155
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
156
     * @throws \Yapeal\Exception\YapealException
157
     *
158
     * @see    setCode()
159
     */
160
    protected function execute(InputInterface $input, OutputInterface $output): int
161
    {
162
        $posts = $this->processPost($input);
163
        $posts['mask'] = $input->getArgument('mask');
164
        $dic = $this->getDic();
165
        $options = $input->getOptions();
166
        $dic['Yapeal.Create.overwrite'] = $options['overwrite'];
167
        if (array_key_exists('configFile', $options)) {
168
            $this->processConfigFile($options['configFile'], $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...
169
        }
170
        return $this->createEveApi($input->getArgument('api_name'), $input->getArgument('section_name'), $posts);
171
    }
172
    /**
173
     * @param InputInterface $input
174
     *
175
     * @return array
176
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
177
     */
178
    protected function processPost(InputInterface $input): array
179
    {
180
        $posts = (array)$input->getArgument('post');
181
        if (0 === count($posts)) {
182
            return [];
183
        }
184
        $arguments = [];
185
        foreach ($posts as $post) {
186
            if (false === strpos($post, '=')) {
187
                continue;
188
            }
189
            list($key, $value) = explode('=', $post);
190
            $arguments[$key] = $value;
191
        }
192
        return $arguments;
193
    }
194
}
195