Completed
Push — feature/test-commands ( 109eae...0365eb )
by Oguzhan
04:24
created

BaseCommand::initialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 7
cp 0.7143
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2.0932
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: oguzu
5
 * Date: 27-3-2017
6
 * Time: 16:52
7
 */
8
9
namespace Pbxg33k\MusicInfo\Command;
10
11
12
use Pbxg33k\MusicInfo\MusicInfo;
13
use Pbxg33k\Traits\HydratableTrait;
14
use Pbxg33k\Traits\PropertyTrait;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\Console\Command\Command;
17
use Symfony\Component\Console\Helper\Table;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Debug\Debug;
22
use Symfony\Component\Yaml\Yaml;
23
24
abstract class BaseCommand extends Command
25
{
26
    const COMMAND_PREFIX = 'music-info';
27
28
    const COMMAND_NAME = 'undefined';
29
30
    const COMMAND_DESCRIPTION = 'Description not set';
31
32
    /**
33
     * @var MusicInfo
34
     */
35
    protected $musicInfo;
36
37 6
    protected function initializeMusicInfo()
38
    {
39 6
        $config = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/config.yml'));
40
41 6
        $this->musicInfo = new MusicInfo($config['music_info']);
42 6
    }
43
44 6
    protected function configure()
45
    {
46 6
        if (!$this->musicInfo) {
47 6
            $this->initializeMusicInfo();
48 6
        }
49
50 6
        $this
51 6
            ->setName(static::COMMAND_PREFIX . ':' . static::COMMAND_NAME)
52 6
            ->setDescription(static::COMMAND_DESCRIPTION)
53 6
            ->addOption('debug', 'd', InputOption::VALUE_NONE, 'Enables debug mode');
54
55 6
        parent::configure();
56 6
    }
57
58 6
    protected function initialize(InputInterface $input, OutputInterface $output)
59 1
    {
60 6
        if ($input->getOption('debug')) {
61
            Debug::enable();
62
        }
63
64 6
        parent::initialize($input, $output);
65 6
    }
66
67
    /**
68
     * @param       $collection
69
     * @param       $columns
70
     * @param Table      $table
71
     * @return Table
72
     */
73 6
    protected function generateTableForSearchResult($collection, $columns, Table $table)
74
    {
75 6
        $table->setHeaders(array_values($columns));
76
77 6
        foreach ($collection as $service => $serviceResult) {
78 6
            $table = $this->generateTableRows($serviceResult, $columns, $table);
79 6
        }
80
81 6
        return $table;
82
    }
83
84
    /**
85
     * @param       $collection
86
     * @param       $columns
87
     * @param Table      $table
88
     * @return Table
89
     */
90 6
    protected function generateTableRows($collection, $columns, Table $table)
91
    {
92 6
        foreach ($collection as $item) {
93 6
            $row = [];
94
95 6
            foreach ($columns as $columnKey => $columnValue) {
96 6
                $row[] = $item->getPropertyValue($columnKey);
97 6
            }
98
99 6
            $table->addRow($row);
100 6
        }
101
102 6
        return $table;
103
    }
104
}
105