Completed
Push — master ( df4948...3b59f8 )
by Michael
02:51
created

EveApiCreator::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
c 6
b 0
f 1
dl 0
loc 24
rs 8.9713
cc 1
eloc 13
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\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
        foreach (['retrieve', 'create', 'transform', 'validate', 'cache'] as $eventName) {
90
            if (false === $this->emitEvents($data, $eventName)) {
91
                return 2;
92
            }
93
        }
94
        return 0;
95
    }
96
    /**
97
     * Configures the current command.
98
     */
99
    protected function configure()
100
    {
101
        $help = <<<'EOF'
102
The <info>%command.full_name%</info> command retrieves the XML data from the Eve Api
103
server and creates Yapeal Eve API Database class, xsd, and sql files for most API types.
104
105
    <info>php %command.full_name% section_name api_name mask [<post>]...</info>
106
107
EXAMPLES:
108
Create Char/AccountBalance class, xsd, and sql files in their respective
109
lib/{EveApi, Xsd, Sql}/Char/ directories.
110
    <info>%command.name% char AccountBalance 1 "keyID=1156" "vCode=abc123"</info>
111
112
EOF;
113
        $this->addConfigFileOption();
114
        $this->addArgument('section_name', InputArgument::REQUIRED, 'Name of Eve Api section to retrieve.')
115
            ->addArgument('api_name', InputArgument::REQUIRED, 'Name of Eve Api to retrieve.')
116
            ->addArgument('mask', InputArgument::REQUIRED, 'Bit mask for Eve Api.')
117
            ->addArgument('post', InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
118
                'Optional list of additional POST parameter(s) to send to server.', [])
119
            ->addOption('overwrite', null, InputOption::VALUE_NONE,
120
                'Causes command to overwrite any existing per Eve API files.')
121
            ->setHelp($help);
122
    }
123
    /** @noinspection PhpMissingParentCallCommonInspection */
124
    /**
125
     * Executes the current command.
126
     *
127
     * @param InputInterface  $input  An InputInterface instance
128
     * @param OutputInterface $output An OutputInterface instance
129
     *
130
     * @return null|int null or 0 if everything went fine, or an error code
131
     * @throws \DomainException
132
     * @throws \LogicException
133
     * @throws \Yapeal\Exception\YapealException
134
     *
135
     * @see    setCode()
136
     */
137
    protected function execute(InputInterface $input, OutputInterface $output)
138
    {
139
        $posts = $this->processPost($input);
140
        $posts['mask'] = $input->getArgument('mask');
141
        $dic = $this->getDic();
142
        $dic['Yapeal.Create.overwrite'] = $input->getOption('overwrite');
143
        if ($input->hasOption('configFile')) {
144
            $this->processConfigFile($input->getOption('configFile'), $dic);
145
        }
146
        return $this->createEveApi($input->getArgument('api_name'), $input->getArgument('section_name'), $posts);
147
    }
148
    /**
149
     * @param InputInterface $input
150
     *
151
     * @return array
152
     */
153
    protected function processPost(InputInterface $input)
154
    {
155
        /**
156
         * @var array $posts
157
         */
158
        $posts = (array)$input->getArgument('post');
159
        if (0 === count($posts)) {
160
            return [];
161
        }
162
        $arguments = [];
163
        foreach ($posts as $post) {
164
            list($key, $value) = explode('=', $post);
165
            $arguments[$key] = $value;
166
        }
167
        return $arguments;
168
    }
169
}
170