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

NetworkCache::execute()   C

Complexity

Conditions 7
Paths 48

Size

Total Lines 48
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 48
rs 6.7272
cc 7
eloc 32
nc 48
nop 2
1
<?php
2
/**
3
 * Contains NetworkCache 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 Symfony\Component\Console\Command\Command;
37
use Symfony\Component\Console\Input\InputArgument;
38
use Symfony\Component\Console\Input\InputInterface;
39
use Symfony\Component\Console\Output\OutputInterface;
40
use Yapeal\CommonToolsTrait;
41
use Yapeal\Container\ContainerInterface;
42
use Yapeal\Event\EveApiEventEmitterTrait;
43
use Yapeal\Log\Logger;
44
use Yapeal\Xml\EveApiReadWriteInterface;
45
46
/**
47
 * Class NetworkCache
48
 */
49
class NetworkCache extends Command
50
{
51
    use CommonToolsTrait, ConfigFileTrait, EveApiEventEmitterTrait, VerbosityToStrategyTrait;
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
        $this->setDescription('Retrieves Eve Api XML from servers and puts it in file');
62
        $this->setName($name);
63
        $this->setDic($dic);
64
        parent::__construct($name);
65
    }
66
    /**
67
     * Configures the current command.
68
     */
69
    protected function configure()
70
    {
71
        $help = <<<'EOF'
72
The <info>%command.full_name%</info> command retrieves the XML data from the
73
Eve Api server and stores it in a file. It will put the file in the normal
74
cache directory per the configuration settings.
75
76
    <info>php %command.full_name% section_name api_name</info>
77
78
EXAMPLES:
79
Save current server status to the cache directory.
80
    <info>%command.name% server ServerStatus</info>
81
82
EOF;
83
        $this->addConfigFileOption();
84
        $this->addArgument('section_name', InputArgument::REQUIRED, 'Name of Eve Api section to retrieve.')
85
            ->addArgument('api_name', InputArgument::REQUIRED, 'Name of Eve Api to retrieve.')
86
            ->addArgument('post',
87
                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
88
                'Optional list of additional POST parameter(s) to send to server.',
89
                [])
90
            ->setHelp($help);
91
    }
92
    /** @noinspection PhpMissingParentCallCommonInspection */
93
    /**
94
     * Executes the current command.
95
     *
96
     * This method is not abstract because you can use this class
97
     * as a concrete class. In this case, instead of defining the
98
     * execute() method, you set the code to execute by passing
99
     * a Closure to the setCode() method.
100
     *
101
     * @param InputInterface  $input  An InputInterface instance
102
     * @param OutputInterface $output An OutputInterface instance
103
     *
104
     * @return int|null null or 0 if everything went fine, or an error code
105
     * @throws \DomainException
106
     * @throws \InvalidArgumentException
107
     * @throws \LogicException
108
     * @throws \Yapeal\Exception\YapealException
109
     *
110
     * @see    setCode()
111
     */
112
    protected function execute(InputInterface $input, OutputInterface $output)
113
    {
114
        /**
115
         * @var \Symfony\Component\Console\Output\Output $output
116
         */
117
        $posts = $this->processPost($input);
118
        $dic = $this->getDic();
119
        $options = $input->getOptions();
120
        if (!empty($options['configFile'])) {
121
            $this->processConfigFile($options['configFile'], $dic);
122
        }
123
        $apiName = $input->getArgument('api_name');
124
        $sectionName = $input->getArgument('section_name');
125
        if (!$this->hasYem()) {
126
            $this->setYem($dic['Yapeal.Event.Mediator']);
127
        }
128
        $this->setLogThresholdFromVerbosity($output);
129
        /**
130
         * Get new Data instance from factory.
131
         *
132
         * @var EveApiReadWriteInterface $data
133
         */
134
        $data = $dic['Yapeal.Xml.Data'];
135
        $data->setEveApiName($apiName)
136
            ->setEveApiSectionName($sectionName)
137
            ->setEveApiArguments($posts);
138
        $mess = 'Starting ' . $this->getName() . ' of';
139
        $mess = $this->createEveApiMessage($mess, $data);
140
        $this->getYem()
141
            ->triggerLogEvent('Yapeal.Log.log', Logger::INFO, $mess);
142
        if ($output::VERBOSITY_QUIET !== $output->getVerbosity()) {
143
            $output->writeln('<info>' . $mess . '</info>');
144
        }
145
        foreach (['retrieve', 'preserve'] as $eventName) {
146
            $this->emitEvents($data, $eventName, 'Yapeal.EveApi.Raw');
147
        }
148
        if (false === $data->getEveApiXml()) {
149
            $mess = 'Could NOT retrieve Eve Api data of';
150
            $mess = $this->createEveApiMessage($mess, $data);
151
            $this->getYem()
152
                ->triggerLogEvent('Yapeal.Log.log', Logger::INFO, $mess);
153
            if ($output::VERBOSITY_QUIET !== $output->getVerbosity()) {
154
                $output->writeln('<error>' . $mess . '</error>');
155
            }
156
            return 2;
157
        }
158
        return 0;
159
    }
160
    /**
161
     * @param InputInterface $input
162
     *
163
     * @return array
164
     */
165
    protected function processPost(InputInterface $input)
166
    {
167
        $posts = (array)$input->getArgument('post');
168
        if (0 === count($posts)) {
169
            return [];
170
        }
171
        $arguments = [];
172
        foreach ($posts as $post) {
173
            if (false === strpos($post, '=')) {
174
                continue;
175
            }
176
            list($key, $value) = explode('=', $post);
177
            $arguments[$key] = $value;
178
        }
179
        return $arguments;
180
    }
181
}
182