Processor::execute()   B
last analyzed

Complexity

Conditions 6
Paths 16

Size

Total Lines 46
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6.027

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 46
ccs 20
cts 22
cp 0.9091
rs 8.4751
cc 6
eloc 22
nc 16
nop 2
crap 6.027
1
<?php
2
3
namespace ADiaz\AML\OpenList\commands;
4
5
use ADiaz\AML\OpenList\utils\Exporter;
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\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * This file is part of the OpenList Parser utility.
14
 *
15
 * @category PHP
16
 *
17
 * @author    Alberto Diaz <[email protected]>
18
 * @copyright 2016 Alberto Diaz <[email protected]>
19
 * @license   This source file is subject to the MIT license that is bundled
20
 *
21
 * @version Release: @package_version@
22
 *
23
 * @link http://tytem.com
24
 */
25
class Processor extends Command
26
{
27
    const FINISH_OK = 'The lists were processed';
28
29
    protected $list_path;
30
    protected $output_path;
31
    protected $controller_namespace;
32
    protected $lists;
33
    protected $date_format;
34
35 1
    public function __construct($conf)
36
    {
37 1
        $this->list_path = $conf['listsPath'];
38 1
        $this->output_path = $conf['outputPath'];
39 1
        $this->controller_namespace = $conf['controllerNamespace'];
40 1
        $this->lists = $conf['lists'];
41 1
        $this->date_format = $conf['dateFormat'];
42
43 1
        parent::__construct();
44 1
    }
45
46 1
    protected function configure()
47
    {
48
        $this
49 1
            ->setName('process')
50 1
            ->setDescription('Process the lists')
51 1
            ->addArgument(
52 1
                'lists',
53 1
                InputArgument::IS_ARRAY,
54 1
                'Which lists do you want to process (separate multiple names with a space)?'
55
            )
56 1
            ->addOption(
57 1
                'disable-serialize',
58 1
                null,
59 1
                InputOption::VALUE_NONE,
60 1
                'If set, the serialized version will not be created'
61
            )
62 1
            ->addOption(
63 1
                'disable-all',
64 1
                null,
65 1
                InputOption::VALUE_NONE,
66 1
                'If set, the combined file will not be created'
67
            );
68 1
    }
69
70 1
    protected function execute(InputInterface $input, OutputInterface $output)
71
    {
72 1
        $exporter = new Exporter($this->output_path, $this->date_format);
73
74 1
        $lists = $input->getArgument('lists');
75
76
        // filter lists
77 1
        if ($lists) {
78
            $this->lists = $this->getLists($lists);
79
        }
80
81 1
        $allEntities = [];
82 1
        foreach ($this->lists as $list) {
83 1
            $this->validateInfo($list);
84
85
            //get instance
86 1
            $classWithNS = $this->controller_namespace.'\\'.$list['class'];
87 1
            $parser = new $classWithNS();
88
89
            // set the location of the sanction file
90 1
            $parser->setSourcePath($this->getFileListPath($list));
91
92 1
            if ($parser->run()) {
93
                // export json
94 1
                $exporter->json($parser->getEntities(), $list['filename']);
95
96
                // export serialize
97 1
                if (!$input->getOption('disable-serialize')) {
98 1
                    $exporter->serialize($parser->getEntities(), $list['filename']);
99
                }
100
101
                // keep the entities to save the combined file
102 1
                $allEntities[] = $parser->getEntities();
103
            } else {
104
                echo 'Error processing the parser '.get_class($parser)."\n";
105
            }
106
107 1
            unset($parser);
108
        }
109
110 1
        if (!$input->getOption('disable-all')) {
111 1
            $exporter->json($allEntities, 'all');
112
        }
113
114 1
        $output->writeln(self::FINISH_OK);
115 1
    }
116
117
    /**
118
     * Get the lists to process.
119
     *
120
     * @param $selectedLists
121
     *
122
     * @return array
123
     */
124
    protected function getLists($selectedLists)
125
    {
126
        $listsToProcess = [];
127
        foreach ($this->lists as $list) {
128
            if (in_array($list['id'], $selectedLists)) {
129
                $listsToProcess[] = $list;
130
            }
131
        }
132
133
        return $listsToProcess;
134
    }
135
136
    /**
137
     * Validate if the list contains the minimun elemenents.
138
     *
139
     * @param $list
140
     */
141 1
    protected function validateInfo($list)
142
    {
143 1
        if (!array_key_exists('id', $list) || !array_key_exists('url', $list) || !array_key_exists('folder', $list) || !array_key_exists('filename', $list) || !array_key_exists('format', $list) || !array_key_exists('class', $list)) {
144
            throw new \LogicException('Please fill the info of the list');
145
        }
146 1
    }
147
148
    /**
149
     * Get the path where are located the lists.
150
     *
151
     * @param array $list
152
     *
153
     * @return string path
154
     */
155 1
    protected function getFileListPath($list)
156
    {
157 1
        return $this->list_path.strtolower($list['id']).DIRECTORY_SEPARATOR.strtolower($list['id']).'.'.$list['format'];
158
    }
159
}
160