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

NetworkRetriever   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 8
dl 0
loc 120
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A configure() 0 21 1
B execute() 0 33 3
A processPost() 0 14 3
1
<?php
2
/**
3
 * Contains NetworkRetriever 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) 2014-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 2014-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 FilePathNormalizer\FilePathNormalizerTrait;
37
use InvalidArgumentException;
38
use LogicException;
39
use Symfony\Component\Console\Command\Command;
40
use Symfony\Component\Console\Input\InputArgument;
41
use Symfony\Component\Console\Input\InputInterface;
42
use Symfony\Component\Console\Input\InputOption;
43
use Symfony\Component\Console\Output\OutputInterface;
44
use Yapeal\Console\CommandToolsTrait;
45
use Yapeal\Container\ContainerInterface;
46
use Yapeal\Event\EveApiEventEmitterTrait;
47
use Yapeal\Exception\YapealConsoleException;
48
use Yapeal\Exception\YapealDatabaseException;
49
use Yapeal\Exception\YapealException;
50
use Yapeal\Log\Logger;
51
use Yapeal\Xml\EveApiReadWriteInterface;
52
53
/**
54
 * Class NetworkRetriever
55
 */
56
class NetworkRetriever extends Command
57
{
58
    use CommandToolsTrait, FilePathNormalizerTrait, EveApiEventEmitterTrait;
59
    /**
60
     * @param string|null        $name
61
     * @param ContainerInterface $dic
62
     *
63
     * @throws InvalidArgumentException
64
     * @throws LogicException
65
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
66
     * @throws \Symfony\Component\Console\Exception\LogicException
67
     */
68
    public function __construct($name, ContainerInterface $dic)
69
    {
70
        $this->setDescription(
71
            'Retrieves Eve Api XML from servers and puts it in file'
72
        );
73
        $this->setName($name);
74
        $this->setDic($dic);
75
        parent::__construct($name);
76
    }
77
    /**
78
     * Configures the current command.
79
     */
80
    protected function configure()
81
    {
82
        $help = <<<'EOF'
83
The <info>%command.full_name%</info> command retrieves the XML data from the Eve Api
84
server and stores it in a file. By default it will put the file in the current
85
working directory.
86
87
    <info>php %command.full_name% section_name api_name</info>
88
89
EXAMPLES:
90
Save current server status in current directory.
91
    <info>%command.name% server ServerStatus</info>
92
93
EOF;
94
        $this->addArgument('section_name', InputArgument::REQUIRED, 'Name of Eve Api section to retrieve.')
95
             ->addArgument('api_name', InputArgument::REQUIRED, 'Name of Eve Api to retrieve.')
96
             ->addArgument('post', InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
97
                 'Optional list of additional POST parameter(s) to send to server.', [])
98
             ->addOption('directory', 'd', InputOption::VALUE_REQUIRED, 'Directory that XML will be sent to.')
99
             ->setHelp($help);
100
    }
101
    /** @noinspection PhpMissingParentCallCommonInspection */
102
    /**
103
     * Executes the current command.
104
     *
105
     * This method is not abstract because you can use this class
106
     * as a concrete class. In this case, instead of defining the
107
     * execute() method, you set the code to execute by passing
108
     * a Closure to the setCode() method.
109
     *
110
     * @param InputInterface  $input  An InputInterface instance
111
     * @param OutputInterface $output An OutputInterface instance
112
     *
113
     * @return int|null null or 0 if everything went fine, or an error code
114
     *
115
     * @throws \DomainException
116
     * @throws \InvalidArgumentException
117
     * @throws \LogicException
118
     * @throws YapealException
119
     * @throws YapealConsoleException
120
     * @throws YapealDatabaseException
121
     * @see    setCode()
122
     */
123
    protected function execute(InputInterface $input, OutputInterface $output)
124
    {
125
        $posts = $this->processPost($input);
126
        $dic = $this->getDic();
127
        $apiName = $input->getArgument('api_name');
128
        $sectionName = $input->getArgument('section_name');
129
        $this->setYem($dic['Yapeal.Event.Mediator']);
130
        $mess = 'Starting Network retrieve';
131
        $this->getYem()
132
             ->triggerLogEvent('Yapeal.Log.log', Logger::ERROR, $mess);
133
        /**
134
         * Get new Data instance from factory.
135
         *
136
         * @var EveApiReadWriteInterface $data
137
         */
138
        $data = $dic['Yapeal.Xml.Data'];
139
        $data->setEveApiName($apiName)
140
             ->setEveApiSectionName($sectionName)
141
             ->setEveApiArguments($posts);
142
        foreach (['retrieve', 'cache'] as $eventName) {
143
            $this->emitEvents($data, $eventName);
144
        }
145
        if (false === $data->getEveApiXml()) {
146
            $mess = sprintf(
147
                '<error>Could NOT retrieve Eve Api data for %1$s/%2$s</error>',
148
                strtolower($sectionName),
149
                $apiName
150
            );
151
            $output->writeln($mess);
152
            return 2;
153
        }
154
        return 0;
155
    }
156
    /**
157
     * @param InputInterface $input
158
     *
159
     * @return array
160
     */
161
    protected function processPost(InputInterface $input)
162
    {
163
        $posts = (array)$input->getArgument('post');
164
        if (0 !== count($posts)) {
165
            $arguments = [];
166
            foreach ($posts as $post) {
167
                list($key, $value) = explode('=', $post);
168
                $arguments[$key] = $value;
169
            }
170
            $posts = $arguments;
171
            return $posts;
172
        }
173
        return $posts;
174
    }
175
}
176