Completed
Push — master ( 4a905b...82874d )
by Michael
02:55
created

EveApiCreator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 141
ccs 0
cts 83
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B createEveApi() 0 31 3
B configure() 0 28 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-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 2015-2016 Michael Cummings
32
 * @license   http://www.gnu.org/copyleft/lesser.html GNU LGPL
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal\Cli\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\Cli\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.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.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
         * TODO: 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.full_name%</info> command retrieves the XML data from the Eve Api
119
server and creates Yapeal Eve API Database class, xsd, and sql files for most API types.
120
121
    <info>php %command.full_name% section_name api_name mask [<post>]...</info>
122
123
EXAMPLES:
124
Create Char/AccountBalance class, xsd, and sql files in their respective
125
lib/{EveApi, Xsd, Sql}/Char/ directories.
126
    <info>%command.name% char AccountBalance 1 "keyID=1156" "vCode=abc123"</info>
127
128
EOF;
129
        $this->addConfigFileOption();
130
        $this->addArgument('section_name', InputArgument::REQUIRED, 'Name of Eve Api section to retrieve.')
131
            ->addArgument('api_name', InputArgument::REQUIRED, 'Name of Eve Api to retrieve.')
132
            ->addArgument('mask', InputArgument::REQUIRED, 'Bit mask for Eve Api.')
133
            ->addArgument('post',
134
                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
135
                'Optional list of additional POST parameter(s) to send to server.',
136
                [])
137
            ->addOption('overwrite',
138
                null,
139
                InputOption::VALUE_NONE,
140
                'Causes command to overwrite any existing per Eve API files.')
141
            ->setHelp($help);
142
    }
143
    /** @noinspection PhpMissingParentCallCommonInspection */
144
    /**
145
     * Executes the current command.
146
     *
147
     * @param InputInterface  $input  An InputInterface instance
148
     * @param OutputInterface $output An OutputInterface instance
149
     *
150
     * @return int null or 0 if everything went fine, or an error code
151
     * @throws \DomainException
152
     * @throws \LogicException
153
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
154
     * @throws \Yapeal\Exception\YapealException
155
     *
156
     * @see    setCode()
157
     */
158
    protected function execute(InputInterface $input, OutputInterface $output): int
159
    {
160
        $posts = $this->processPost($input);
161
        $posts['mask'] = $input->getArgument('mask');
162
        $dic = $this->getDic();
163
        $options = $input->getOptions();
164
        $dic['Yapeal.Create.overwrite'] = $options['overwrite'];
165
        if (array_key_exists('configFile', $options)) {
166
            $this->processConfigFile($options['configFile'], $dic);
167
        }
168
        return $this->createEveApi($input->getArgument('api_name'), $input->getArgument('section_name'), $posts);
169
    }
170
    /**
171
     * @param InputInterface $input
172
     *
173
     * @return array
174
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
175
     */
176
    protected function processPost(InputInterface $input): array
177
    {
178
        $posts = (array)$input->getArgument('post');
179
        if (0 === count($posts)) {
180
            return [];
181
        }
182
        $arguments = [];
183
        foreach ($posts as $post) {
184
            if (false === strpos($post, '=')) {
185
                continue;
186
            }
187
            list($key, $value) = explode('=', $post);
188
            $arguments[$key] = $value;
189
        }
190
        return $arguments;
191
    }
192
}
193