Completed
Push — master ( 7c429e...75748b )
by Michael
03:23
created

EveApiCreator::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 23
rs 9.0856
cc 1
eloc 12
nc 1
nop 0
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\Console\CommandToolsTrait;
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 CommandToolsTrait, 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
        parent::__construct($name);
67
    }
68
    /**
69
     * @param string $apiName
70
     * @param string $sectionName
71
     * @param array  $posts
72
     *
73
     * @return int
74
     *
75
     * @throws \DomainException
76
     * @throws \InvalidArgumentException
77
     * @throws \LogicException
78
     * @throws \Yapeal\Exception\YapealConsoleException
79
     * @throws \Yapeal\Exception\YapealDatabaseException
80
     * @throws \Yapeal\Exception\YapealException
81
     */
82
    public function createEveApi($apiName, $sectionName, $posts)
83
    {
84
        /**
85
         * Get new Data instance from factory.
86
         *
87
         * @var EveApiReadWriteInterface $data
88
         */
89
        $data = $this->getDic()['Yapeal.Xml.Data'];
90
        $data->setEveApiName($apiName)
91
             ->setEveApiSectionName($sectionName)
92
             ->setEveApiArguments($posts);
93
        foreach (['retrieve', 'create', 'transform', 'validate', 'cache'] as $eventName) {
94
            if (false === $this->emitEvents($data, $eventName)) {
95
                return 2;
96
            }
97
        }
98
        return 0;
99
    }
100
    /**
101
     * Configures the current command.
102
     */
103
    protected function configure()
104
    {
105
        $help = <<<'EOF'
106
The <info>%command.full_name%</info> command retrieves the XML data from the Eve Api
107
server and creates Yapeal Eve API Database class, xsd, and sql files for most API types.
108
109
    <info>php %command.full_name% section_name api_name mask [<post>]...</info>
110
111
EXAMPLES:
112
Create Char/AccountBalance class, xsd, and sql files in their respective
113
lib/{EveApi, Xsd, Sql}/Char/ directories.
114
    <info>%command.name% char AccountBalance 1 "keyID=1156" "vCode=abc123"</info>
115
116
EOF;
117
        $this->addArgument('section_name', InputArgument::REQUIRED, 'Name of Eve Api section to retrieve.')
118
             ->addArgument('api_name', InputArgument::REQUIRED, 'Name of Eve Api to retrieve.')
119
             ->addArgument('mask', InputArgument::REQUIRED, 'Bit mask for Eve Api.')
120
             ->addArgument('post', InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
121
                 'Optional list of additional POST parameter(s) to send to server.', [])
122
             ->addOption('overwrite', null, InputOption::VALUE_NONE,
123
                 'Causes command to overwrite any existing per Eve API files.')
124
             ->setHelp($help);
125
    }
126
    /** @noinspection PhpMissingParentCallCommonInspection */
127
    /**
128
     * Executes the current command.
129
     *
130
     * @param InputInterface  $input  An InputInterface instance
131
     * @param OutputInterface $output An OutputInterface instance
132
     *
133
     * @return null|int null or 0 if everything went fine, or an error code
134
     *
135
     * @throws \DomainException
136
     * @throws \InvalidArgumentException
137
     * @throws \LogicException
138
     * @throws \Yapeal\Exception\YapealConsoleException
139
     * @throws \Yapeal\Exception\YapealDatabaseException
140
     * @throws \Yapeal\Exception\YapealException
141
     * @see    setCode()
142
     */
143
    protected function execute(InputInterface $input, OutputInterface $output)
144
    {
145
        $posts = $this->processPost($input);
146
        $posts['mask'] = $input->getArgument('mask');
147
        $dic = $this->getDic();
148
        if ($input->hasOption('overwrite')) {
149
            $dic['Yapeal.Create.overwrite'] = true;
150
        }
151
        $this->setYem($dic['Yapeal.Event.Mediator']);
152
        $apiName = $input->getArgument('api_name');
153
        $sectionName = $input->getArgument('section_name');
154
        return $this->createEveApi($apiName, $sectionName, $posts);
155
    }
156
    /**
157
     * @param InputInterface $input
158
     *
159
     * @return array
160
     */
161
    protected function processPost(InputInterface $input)
162
    {
163
        /**
164
         * @var array $posts
165
         */
166
        $posts = (array)$input->getArgument('post');
167
        if (0 === count($posts)) {
168
            return [];
169
        }
170
        $arguments = [];
171
        foreach ($posts as $post) {
172
            list($key, $value) = explode('=', $post);
173
            $arguments[$key] = $value;
174
        }
175
        return $arguments;
176
    }
177
}
178