Completed
Push — master ( 4a905b...82874d )
by Michael
02:55
created

NetworkCache   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
c 1
b 0
f 1
lcom 1
cbo 9
dl 0
loc 132
ccs 0
cts 83
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 23 1
C execute() 0 45 7
A processPost() 0 16 4
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains NetworkCache class.
5
 *
6
 * PHP version 7.0+
7
 *
8
 * LICENSE:
9
 * This file is part of Yet Another Php Eve Api Library also know as Yapeal
10
 * which can be used to access the Eve Online API data and place it into a
11
 * database.
12
 * Copyright (C) 2014-2016 Michael Cummings
13
 *
14
 * This program is free software: you can redistribute it and/or modify it
15
 * under the terms of the GNU Lesser General Public License as published by the
16
 * Free Software Foundation, either version 3 of the License, or (at your
17
 * option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful, but WITHOUT
20
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
22
 * for more details.
23
 *
24
 * You should have received a copy of the GNU Lesser General Public License
25
 * along with this program. If not, see
26
 * <http://spdx.org/licenses/LGPL-3.0.html>.
27
 *
28
 * You should be able to find a copy of this license in the COPYING-LESSER.md
29
 * file. A copy of the GNU GPL should also be available in the COPYING.md file.
30
 *
31
 * @copyright 2014-2016 Michael Cummings
32
 * @license   http://www.gnu.org/copyleft/lesser.html GNU LGPL
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal\Cli\Network;
36
37
use Symfony\Component\Console\Command\Command;
38
use Symfony\Component\Console\Input\InputArgument;
39
use Symfony\Component\Console\Input\InputInterface;
40
use Symfony\Component\Console\Output\OutputInterface;
41
use Yapeal\Cli\ConfigFileTrait;
42
use Yapeal\Cli\VerbosityToStrategyTrait;
43
use Yapeal\CommonToolsTrait;
44
use Yapeal\Container\ContainerInterface;
45
use Yapeal\Event\EveApiEventEmitterTrait;
46
use Yapeal\Log\Logger;
47
use Yapeal\Xml\EveApiReadWriteInterface;
48
49
/**
50
 * Class NetworkCache
51
 */
52
class NetworkCache extends Command
53
{
54
    use CommonToolsTrait, ConfigFileTrait, EveApiEventEmitterTrait, VerbosityToStrategyTrait;
55
    /**
56
     * @param string|null        $name
57
     * @param ContainerInterface $dic
58
     *
59
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
60
     * @throws \Symfony\Component\Console\Exception\LogicException
61
     */
62
    public function __construct($name, ContainerInterface $dic)
63
    {
64
        $this->setDescription('Retrieves Eve Api XML from servers and puts it in file');
65
        $this->setName($name);
66
        $this->setDic($dic);
67
        parent::__construct($name);
68
    }
69
    /**
70
     * Configures the current command.
71
     */
72
    protected function configure()
73
    {
74
        $help = <<<'EOF'
75
The <info>%command.full_name%</info> command retrieves the XML data from the
76
Eve Api server and stores it in a file. It will put the file in the normal
77
cache directory per the configuration settings.
78
79
    <info>php %command.full_name% section_name api_name</info>
80
81
EXAMPLES:
82
Save current server status to the cache directory.
83
    <info>%command.name% server ServerStatus</info>
84
85
EOF;
86
        $this->addConfigFileOption();
87
        $this->addArgument('section_name', InputArgument::REQUIRED, 'Name of Eve Api section to retrieve.')
88
            ->addArgument('api_name', InputArgument::REQUIRED, 'Name of Eve Api to retrieve.')
89
            ->addArgument('post',
90
                InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
91
                'Optional list of additional POST parameter(s) to send to server.',
92
                [])
93
            ->setHelp($help);
94
    }
95
    /** @noinspection PhpMissingParentCallCommonInspection */
96
    /**
97
     * Executes the current command.
98
     *
99
     * This method is not abstract because you can use this class
100
     * as a concrete class. In this case, instead of defining the
101
     * execute() method, you set the code to execute by passing
102
     * a Closure to the setCode() method.
103
     *
104
     * @param InputInterface  $input  An InputInterface instance
105
     * @param OutputInterface $output An OutputInterface instance
106
     *
107
     * @return int|null null or 0 if everything went fine, or an error code
108
     * @throws \DomainException
109
     * @throws \InvalidArgumentException
110
     * @throws \LogicException
111
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
112
     * @throws \Yapeal\Exception\YapealException
113
     *
114
     * @see    setCode()
115
     */
116
    protected function execute(InputInterface $input, OutputInterface $output)
117
    {
118
        $posts = $this->processPost($input);
119
        $dic = $this->getDic();
120
        $options = $input->getOptions();
121
        if (!empty($options['configFile'])) {
122
            $this->processConfigFile($options['configFile'], $dic);
123
        }
124
        $apiName = $input->getArgument('api_name');
125
        $sectionName = $input->getArgument('section_name');
126
        if (!$this->hasYem()) {
127
            $this->setYem($dic['Yapeal.Event.Mediator']);
128
        }
129
        $this->setLogThresholdFromVerbosity($output);
130
        /**
131
         * Get new Data instance from factory.
132
         *
133
         * @var EveApiReadWriteInterface $data
134
         */
135
        $data = $dic['Yapeal.Xml.Data'];
136
        $data->setEveApiName($apiName)
137
            ->setEveApiSectionName($sectionName)
138
            ->setEveApiArguments($posts);
139
        if ($output::VERBOSITY_QUIET !== $output->getVerbosity()) {
140
            $mess = sprintf('<info>Starting %1$s of', $this->getName());
141
            $mess = $this->createEveApiMessage($mess, $data) . '</info>';
142
            $this->getYem()
143
                ->triggerLogEvent('Yapeal.Log.log', Logger::INFO, strip_tags($mess));
144
            $output->writeln($mess);
145
        }
146
        foreach (['retrieve', 'preserve'] as $eventName) {
147
            $this->emitEvents($data, $eventName, 'Yapeal.EveApi.Raw');
148
        }
149
        if (false === $data->getEveApiXml()) {
150
            if ($output::VERBOSITY_QUIET !== $output->getVerbosity()) {
151
                $mess = '<error>Could NOT retrieve Eve Api data of';
152
                $mess = $this->createEveApiMessage($mess, $data) . '</error>';
153
                $this->getYem()
154
                    ->triggerLogEvent('Yapeal.Log.log', Logger::INFO, strip_tags($mess));
155
                $output->writeln($mess);
156
            }
157
            return 2;
158
        }
159
        return 0;
160
    }
161
    /**
162
     * @param InputInterface $input
163
     *
164
     * @return array
165
     * @throws \Symfony\Component\Console\Exception\InvalidArgumentException
166
     */
167
    protected function processPost(InputInterface $input)
168
    {
169
        $posts = (array)$input->getArgument('post');
170
        if (0 === count($posts)) {
171
            return [];
172
        }
173
        $arguments = [];
174
        foreach ($posts as $post) {
175
            if (false === strpos($post, '=')) {
176
                continue;
177
            }
178
            list($key, $value) = explode('=', $post);
179
            $arguments[$key] = $value;
180
        }
181
        return $arguments;
182
    }
183
}
184