Completed
Push — master ( 70fc1a...2d4410 )
by Michael
02:52
created

EveApiCreator::createEveApi()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 3
eloc 15
nc 3
nop 3
1
<?php
2
/**
3
 * Contains EveApiCreator class.
4
 *
5
 * PHP version 5.5
6
 *
7
 * LICENSE:
8
 * This file is part of Yet Another Php Eve Api Library also know as Yapeal
9
 * which can be used to access the Eve Online API data and place it into a
10
 * database.
11
 * Copyright (C) 2015-2016 Michael Cummings
12
 *
13
 * This program is free software: you can redistribute it and/or modify it
14
 * under the terms of the GNU Lesser General Public License as published by the
15
 * Free Software Foundation, either version 3 of the License, or (at your
16
 * option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful, but WITHOUT
19
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
21
 * for more details.
22
 *
23
 * You should have received a copy of the GNU Lesser General Public License
24
 * along with this program. If not, see
25
 * <http://www.gnu.org/licenses/>.
26
 *
27
 * You should be able to find a copy of this license in the LICENSE.md file. A
28
 * copy of the GNU GPL should also be available in the GNU-GPL.md file.
29
 *
30
 * @copyright 2015-2016 Michael Cummings
31
 * @license   http://www.gnu.org/copyleft/lesser.html GNU LGPL
32
 * @author    Michael Cummings <[email protected]>
33
 */
34
namespace Yapeal\Console\Command;
35
36
use Symfony\Component\Console\Command\Command;
37
use Symfony\Component\Console\Input\InputArgument;
38
use Symfony\Component\Console\Input\InputInterface;
39
use Symfony\Component\Console\Input\InputOption;
40
use Symfony\Component\Console\Output\OutputInterface;
41
use Yapeal\CommonToolsTrait;
42
use Yapeal\Container\ContainerInterface;
43
use Yapeal\Event\EveApiEventEmitterTrait;
44
use Yapeal\Xml\EveApiReadWriteInterface;
45
46
/**
47
 * Class EveApiCreator
48
 */
49
class EveApiCreator extends Command
50
{
51
    use CommonToolsTrait, ConfigFileTrait, EveApiEventEmitterTrait;
52
    /**
53
     * @param string|null        $name
54
     * @param ContainerInterface $dic
55
     *
56
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
57
     * @throws \Symfony\Component\Console\Exception\LogicException
58
     */
59
    public function __construct($name, ContainerInterface $dic)
60
    {
61
        $desc = 'Retrieves Eve Api XML from CCP servers and creates database class, XSD, and SQL files based on the XML'
62
            . ' structure received';
63
        $this->setDescription($desc);
64
        $this->setName($name);
65
        $this->setDic($dic);
66
        $this->setYem($dic['Yapeal.Event.Mediator']);
67
        parent::__construct($name);
68
    }
69
    /**
70
     * @param string $apiName
71
     * @param string $sectionName
72
     * @param array  $posts
73
     *
74
     * @return int
75
     * @throws \LogicException
76
     *
77
     */
78
    public function createEveApi($apiName, $sectionName, $posts)
79
    {
80
        /**
81
         * Get new Data instance from factory.
82
         *
83
         * @var EveApiReadWriteInterface $data
84
         */
85
        $data = $this->getDic()['Yapeal.Xml.Data'];
86
        $data->setEveApiName($apiName)
87
            ->setEveApiSectionName($sectionName)
88
            ->setEveApiArguments($posts);
89
        /*
90
         * Create can't use pre-processed Eve Api XML, it needs to be unaltered version directly from the servers.
91
         * For now use Raw preserve since DB tables are not added automatically which would cause normal preservers to
92
         * fail and add junk to the logs.
93
         * TODO: Need to decide if implementing a hybrid SQL update/init system is worth doing.
94
         */
95
        $events = [
96
            'retrieve' => 'Yapeal.EveApi.Raw',
97
            'create' => 'Yapeal.EveApi',
98
            'transform' => 'Yapeal.EveApi',
99
            'validate' => 'Yapeal.EveApi',
100
            'preserve' => 'Yapeal.EveApi.Raw'
101
        ];
102
        foreach ($events as $eventName => $eventPrefix) {
103
            if (false === $this->emitEvents($data, $eventName, $eventPrefix)) {
104
                return 2;
105
            }
106
        }
107
        return 0;
108
    }
109
    /**
110
     * Configures the current command.
111
     */
112
    protected function configure()
113
    {
114
        $help = <<<'EOF'
115
The <info>%command.full_name%</info> command retrieves the XML data from the Eve Api
116
server and creates Yapeal Eve API Database class, xsd, and sql files for most API types.
117
118
    <info>php %command.full_name% section_name api_name mask [<post>]...</info>
119
120
EXAMPLES:
121
Create Char/AccountBalance class, xsd, and sql files in their respective
122
lib/{EveApi, Xsd, Sql}/Char/ directories.
123
    <info>%command.name% char AccountBalance 1 "keyID=1156" "vCode=abc123"</info>
124
125
EOF;
126
        $this->addConfigFileOption();
127
        $this->addArgument('section_name', InputArgument::REQUIRED, 'Name of Eve Api section to retrieve.')
128
            ->addArgument('api_name', InputArgument::REQUIRED, 'Name of Eve Api to retrieve.')
129
            ->addArgument('mask', InputArgument::REQUIRED, 'Bit mask for Eve Api.')
130
            ->addArgument('post',
131
                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
132
                'Optional list of additional POST parameter(s) to send to server.',
133
                [])
134
            ->addOption('overwrite',
135
                null,
136
                InputOption::VALUE_NONE,
137
                'Causes command to overwrite any existing per Eve API files.')
138
            ->setHelp($help);
139
    }
140
    /** @noinspection PhpMissingParentCallCommonInspection */
141
    /**
142
     * Executes the current command.
143
     *
144
     * @param InputInterface  $input  An InputInterface instance
145
     * @param OutputInterface $output An OutputInterface instance
146
     *
147
     * @return null|int null or 0 if everything went fine, or an error code
148
     * @throws \DomainException
149
     * @throws \LogicException
150
     * @throws \Yapeal\Exception\YapealException
151
     *
152
     * @see    setCode()
153
     */
154
    protected function execute(InputInterface $input, OutputInterface $output)
155
    {
156
        $posts = $this->processPost($input);
157
        $posts['mask'] = $input->getArgument('mask');
158
        $dic = $this->getDic();
159
        $options = $input->getOptions();
160
        $dic['Yapeal.Create.overwrite'] = $options['overwrite'];
161
        if (array_key_exists('configFile', $options)) {
162
            $this->processConfigFile($options['configFile'], $dic);
163
        }
164
        return $this->createEveApi($input->getArgument('api_name'), $input->getArgument('section_name'), $posts);
165
    }
166
    /**
167
     * @param InputInterface $input
168
     *
169
     * @return array
170
     */
171
    protected function processPost(InputInterface $input)
172
    {
173
        $posts = (array)$input->getArgument('post');
174
        if (0 === count($posts)) {
175
            return [];
176
        }
177
        $arguments = [];
178
        foreach ($posts as $post) {
179
            if (false === strpos($post, '=')) {
180
                continue;
181
            }
182
            list($key, $value) = explode('=', $post);
183
            $arguments[$key] = $value;
184
        }
185
        return $arguments;
186
    }
187
}
188