Completed
Pull Request — master (#5)
by Arnaud
03:36
created

AbstractCommand::loadConfigurationFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 12
1
<?php
2
3
namespace JK\SamBundle\Command;
4
5
use Exception;
6
use JK\Sam\Filter\FilterBuilder;
7
use JK\Sam\Filter\FilterInterface;
8
use JK\Sam\Task\Task;
9
use JK\Sam\Task\TaskBuilder;
10
use JK\SamBundle\Configuration\Loader\ConfigurationLoader;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17
use Symfony\Component\Yaml\Yaml;
18
19
abstract class AbstractCommand extends Command implements ContainerAwareInterface
20
{
21
    /**
22
     * @var SymfonyStyle
23
     */
24
    protected $io;
25
26
    /**
27
     * @var boolean
28
     */
29
    protected $debug = false;
30
31
    /**
32
     * @var ContainerInterface
33
     */
34
    protected $container;
35
    
36
    /**
37
     * @var EventDispatcherInterface
38
     */
39
    protected $eventDispatcher;
40
41
    /**
42
     * Build tasks from the configuration array.
43
     *
44
     * @param array $configuration
45
     *
46
     * @return Task[]
47
     */
48 1 View Code Duplication
    protected function buildTasks(array $configuration)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50 1
        $this->io->text('- Building tasks...');
51 1
        $builder = new TaskBuilder($this->debug);
52
53 1
        $tasks = $builder->build($configuration);
54
55 1
        $this->io->text('- Tasks build !');
56 1
        $this->io->newLine();
57
58 1
        return $tasks;
59
    }
60
61
    /**
62
     * Build the filter according to the configuration array.
63
     *
64
     * @param array $configuration
65
     *
66
     * @return FilterInterface[]
67
     */
68 1 View Code Duplication
    protected function buildFilters(array $configuration)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70 1
        $this->io->text('- Building filters...');
71 1
        $builder = new FilterBuilder($this->eventDispatcher);
72
73 1
        $filters = $builder->build($configuration);
74
75 1
        $this->io->text('- Filters build !');
76 1
        $this->io->newLine();
77
78 1
        return $filters;
79
    }
80
81
    /**
82
     * Load the configuration from a yml file.
83
     *
84
     * @param $configurationFile
85
     *
86
     * @return string[]
87
     *
88
     * @throws Exception
89
     */
90
    protected function loadConfigurationFile($configurationFile)
91
    {
92
        if (!file_exists($configurationFile)) {
93
            throw new Exception('The configuration yml file '.$configurationFile.' was not found');
94
        }
95
        $configuration = Yaml::parse(file_get_contents($configurationFile));
96
97
        if (empty($configuration['jk_assets']['tasks'])) {
98
            throw new Exception('Tasks not found in configuration file '.$configurationFile);
99
        }
100
101
        return $configuration['jk_assets']['tasks'];
102
    }
103
    
104
    /**
105
     * Load the configuration from a yml file or the container, according to the given option.
106
     *
107
     * @param InputInterface $input
108
     *
109
     * @return array
110
     *
111
     * @throws Exception
112
     */
113 1
    protected function loadConfiguration(InputInterface $input)
114
    {
115 1
        $loader = new ConfigurationLoader();
116
117 1
        if ($input->hasOption('config') && $file = $input->getOption('config')) {
118
            $configuration = $loader->loadFromFile($file);
119
        } else {
120
            
121 1
            if (null === $this->container) {
122
                throw new Exception('Unable to find the assets configuration from the container');
123
            }
124 1
            $configuration = $loader->loadFromContainer($this->container);
125
        }
126
127 1
        return $configuration;
128
    }
129
130
    /**
131
     * Sets the container.
132
     *
133
     * @param ContainerInterface|null $container A ContainerInterface instance or null
134
     */
135 1
    public function setContainer(ContainerInterface $container = null)
136
    {
137 1
        $this->container = $container;
138 1
        $this->eventDispatcher = $this->container->get('event_dispatcher');
139 1
    }
140
}
141