Receiver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ADiaz\AML\OpenList\commands;
4
5
use ADiaz\AML\OpenList\Utils\Utils;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * This file is part of the OpenList Parser utility.
13
 *
14
 * @category PHP
15
 *
16
 * @author    Alberto Diaz <[email protected]>
17
 * @copyright 2016 Alberto Diaz <[email protected]>
18
 * @license   This source file is subject to the MIT license that is bundled
19
 *
20
 * @version Release: @package_version@
21
 *
22
 * @link http://tytem.com
23
 */
24
class Receiver extends Command
25
{
26
    const FINISH_OK = 'The lists were downloaded';
27
28
    protected $list_path;
29
    protected $lists;
30
    protected $date_format;
31
32
    /**
33
     * Receiver constructor.
34
     *
35
     * @param null|string $conf
36
     */
37 2
    public function __construct($conf)
38
    {
39 2
        $this->list_path = $conf['listsPath'];
40 2
        $this->date_format = $conf['dateFormat'];
41 2
        $this->lists = (array) $conf['lists'];
42
43 2
        parent::__construct();
44 2
    }
45
46
    /**
47
     * Configure the arguments and options.
48
     */
49 2
    protected function configure()
50
    {
51
        $this
52 2
            ->setName('receive')
53 2
            ->setDescription('Receive the lists')
54 2
            ->addArgument(
55 2
                'lists',
56 2
                InputArgument::IS_ARRAY,
57 2
                'Which lists do you want to process (separate multiple names with a space)?'
58
            );
59 2
    }
60
61
    /**
62
     * Get the lists.
63
     *
64
     * @param InputInterface  $input
65
     * @param OutputInterface $output
66
     */
67 2
    protected function execute(InputInterface $input, OutputInterface $output)
68
    {
69 2
        $lists = $input->getArgument('lists');
70
71 2
        if ($lists) {
72
            $this->lists = $this->getLists($lists);
73
        }
74
75 2
        $this->createFolders($this->lists);
76
77 2
        $this->archive($this->lists);
78
79 2
        $this->downloadFiles($this->lists);
80
81 2
        $this->verifyFiles($this->lists);
82
83 2
        $output->writeln(self::FINISH_OK);
84 2
    }
85
86
    /**
87
     * Get the lists to process.
88
     *
89
     * @param $selectedLists
90
     *
91
     * @return array
92
     */
93
    protected function getLists($selectedLists)
94
    {
95
        $listsToProcess = [];
96
        foreach ($this->lists as $list) {
97
            if (in_array($list['id'], $selectedLists)) {
98
                $listsToProcess[] = $list;
99
            }
100
        }
101
102
        return $listsToProcess;
103
    }
104
105
    /**
106
     * It creates the folders for the lists if they don't exist already.
107
     *
108
     * @param array $listsInfo
109
     */
110 2
    protected function createFolders(array $listsInfo)
111
    {
112 2
        foreach ($listsInfo as $list) {
113 2
            if (is_dir($this->list_path.$list['folder']) === false) {
114 2
                mkdir($this->list_path.$list['folder']);
115
            }
116
        }
117 2
    }
118
119
    /**
120
     * Archives the existing lists.
121
     *
122
     * @param array $listsInfo
123
     */
124 2
    protected function archive($listsInfo)
125
    {
126 2
        foreach ($listsInfo as $list) {
127 2
            if (file_exists($this->getFilePath($list))) {
128
                //compress
129 1
                Utils::gzCompressFile($this->getFilePath($list), $this->getArchiveName($this->getFilePath($list)));
130
                //delete
131 2
                unlink($this->getFilePath($list));
132
            }
133
        }
134 2
    }
135
136
    /**
137
     * Downloads the lists.
138
     *
139
     * @param array $listsInfo
140
     * @Todo  Check encoding with mbstring and take a look at http://stackoverflow.com/questions/505562/detect-file-encoding-in-php#answer-505582
141
     */
142 2
    protected function downloadFiles($listsInfo)
143
    {
144 2
        foreach ($listsInfo as $list) {
145 2
            file_put_contents($this->getFilePath($list), file_get_contents($list['url']));
146
        }
147 2
    }
148
149
    /**
150
     * Verifies the lists were downloaded and exists.
151
     *
152
     * @param array $listsInfo
153
     */
154 2
    protected function verifyFiles($listsInfo)
155
    {
156 2
        foreach ($listsInfo as $list) {
157 2
            if (!file_exists($this->getFilePath($list))) {
158 2
                echo "The was a problem verifying the list {$list['name']}. The file {$this->getFilePath($list)} doesn't exist";
159
            }
160
        }
161 2
    }
162
163
    /**
164
     * Gets the archive name from a filepath.
165
     *
166
     * @param string $filePath
167
     *
168
     * @return string
169
     */
170 1
    protected function getArchiveName($filePath)
171
    {
172 1
        $fileName = basename($filePath);
173 1
        $newFileName = date($this->date_format).'_'.$fileName;
174
175 1
        return str_replace($fileName, $newFileName, $filePath);
176
    }
177
178
    /**
179
     * Get the filename from a list.
180
     *
181
     * @param array $list
182
     *
183
     * @return string
184
     */
185 2
    protected function getFilename($list)
186
    {
187 2
        return $list['filename'].'.'.$list['format'];
188
    }
189
190
    /**
191
     * Get the filepath of the list.
192
     *
193
     * @param array $list
194
     *
195
     * @return string
196
     */
197 2
    protected function getFilePath($list)
198
    {
199 2
        return $this->list_path.$list['folder'].'/'.$this->getFilename($list);
200
    }
201
}
202