Completed
Push — master ( 360bc1...86b6c1 )
by Michael
02:47
created

NetworkCache::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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