Completed
Pull Request — master (#37)
by Andreas
01:34
created

AbstractParseEvents::execute()   C

Complexity

Conditions 9
Paths 129

Size

Total Lines 85

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 6.5783
c 0
b 0
f 0
cc 9
nc 129
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright (c) 2015-2016 Andreas Heigl<[email protected]>
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @author    Andreas Heigl<[email protected]>
24
 * @copyright 2015-2016 Andreas Heigl/callingallpapers.com
25
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
26
 * @version   0.0
27
 * @since     01.12.2015
28
 * @link      http://github.com/heiglandreas/callingallpapers-cli
29
 */
30
namespace Callingallpapers\Command;
31
32
use Callingallpapers\CfpFilter\FilterList;
33
use Callingallpapers\CfpFilter\FollowUriRedirect;
34
use Callingallpapers\CfpFilter\StripParamsFromUri;
35
use Callingallpapers\ErrorHandling\ErrorStore;
36
use Callingallpapers\Exception\UnverifiedUriException;
37
use Callingallpapers\Parser\ConfsTech\ConferenceParser;
38
use Callingallpapers\Parser\ConfsTechParser;
39
use Callingallpapers\Parser\JoindinCfpParser;
40
use Callingallpapers\Parser\Lanyrd\LanyrdCfpParser;
41
use Callingallpapers\Parser\PapercallIo\PapercallIoParserFactory;
42
use Callingallpapers\Parser\ParserInterface;
43
use Callingallpapers\ResultKeeper\ConsoleOutputResultKeeper;
44
use Callingallpapers\ResultKeeper\ResultKeeper;
45
use Callingallpapers\Service\ConfsTechParserFactory;
46
use Callingallpapers\Service\GeolocationService;
47
use Callingallpapers\Service\ServiceContainer;
48
use Callingallpapers\Service\TimezoneService;
49
use Callingallpapers\Writer\ApiCfpWriter;
50
use GuzzleHttp\Client;
51
use Symfony\Component\Console\Command\Command;
52
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
53
use Symfony\Component\Console\Input\InputInterface;
54
use Symfony\Component\Console\Input\InputOption;
55
use Symfony\Component\Console\Output\OutputInterface;
56
use Symfony\Component\Console\Style\SymfonyStyle;
57
58
abstract class AbstractParseEvents extends Command
59
{
60
    protected function configure()
61
    {
62
        $this->setName(sprintf("parseCfPs:%s", $this->getParserId()))
63
            ->setDescription(sprintf("Retrieve CfPs from %s only and parse them", $this->getParserName()))
64
            ->setDefinition(array(
65
                new InputOption('start', 's', InputOption::VALUE_OPTIONAL, 'What should be the first date to be taken into account?', ''),
66
            ))
67
            ->setHelp(sprintf(<<<EOT
68
Get details about CfPs from https://sesionize.com
69
70
Usage:
71
72
<info>callingallpapers parseCfPs:sessionize 2015-02-23<env></info>
73
74
If you ommit the date the current date will be used instead
75
<info>callingallpapers parseCfPs:sessionize<env></info>
76
EOT
77
            , $this->getServiceUrl()));
78
    }
79
80
    protected function execute(InputInterface $input, OutputInterface $output)
81
    {
82
        $header_style = new OutputFormatterStyle('white', 'green', array('bold'));
83
        $style = new SymfonyStyle($input, $output);
84
        $output->getFormatter()->setStyle('header', $header_style);
85
86
        $start = new \DateTime($input->getOption('start'));
87
88
        if (! $start instanceof \DateTime) {
89
            throw new \InvalidArgumentException('The given date could not be parsed');
90
        }
91
92
        $config = parse_ini_file(__DIR__ . '/../../config/callingallpapers.ini');
93
94
        $client = new Client([
95
            'headers' => [
96
                'Accept' => 'application/json',
97
                'Authorization' => 'token ' . $config['github.token'],
98
            ],
99
        ]);
100
101
        $resultKeeper = new ConsoleOutputResultKeeper($style);
102
        $writer = new ApiCfpWriter($config['event_api_url'], $config['event_api_token'], $client);
103
        $writer->setOutput($output);
104
        $writer->setResultKeeper($resultKeeper);
105
106
        $style->comment(sprintf('Starting to parse %s', $this->getParserName()));
107
108
        // Set CfP-Filters
109
        $filter = new FilterList();
110
        $filter->add(new FollowUriRedirect(['conferenceUri'], $client));
111
        $filter->add(new StripParamsFromUri(['conferenceUri']));
112
        $writer->setFilter($filter);
113
114
        $timezoneService = new TimezoneService($client, $config['timezonedb_token']);
115
        $geolocationService = new GeolocationService($client);
116
117
        // Parse Confs.tech
118
        $parser = $this->getParser(new ServiceContainer($timezoneService, $geolocationService, $client));
119
        $parser->parse($writer);
120
121
        $style->newLine(2);
122
123
        $items = [];
124
        foreach ($resultKeeper->getCreated() as $created) {
125
            $items[] = $created->getConference();
126
        }
127
128
        if ($items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
129
            $style->writeln('Added these new Conferences:');
130
            $style->listing($items);
131
        }
132
133
        $items = [];
134
        foreach ($resultKeeper->getUpdated() as $updated) {
135
            $items[] = $updated->getConference();
136
        }
137
138
        if ($items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
139
            $style->writeln('Updated these Conferences:');
140
            $style->listing($items);
141
        }
142
143
        $items = [];
144
        foreach ($resultKeeper->getFailed() as $failed) {
145
            $items[] = sprintf(
146
                '%1$s (%2$s)',
147
                $failed->getConference(),
148
                $failed->getMessage()
149
            );
150
        }
151
152
        foreach ($resultKeeper->getErrored() as $errored) {
153
            $items[] = sprintf(
154
                '%1$s (%2$s)',
155
                $errored->getConference(),
156
                $errored->getMessage()
157
            );
158
        }
159
160
        if ($items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
161
            $style->writeln('There where issues with these conferences:');
162
            $style->listing($items);
163
        }
164
    }
165
166
    abstract protected function getParser(ServiceContainer $serviceContainer) : ParserInterface;
167
168
    abstract protected function getParserName() : string;
169
170
    abstract protected function getParserId() : string;
171
172
    abstract protected function getServiceUrl() : string;
173
}
174