AbstractCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 122
ccs 24
cts 33
cp 0.7272
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfigurationFile() 0 13 3
A loadConfiguration() 0 16 4
A setContainer() 0 5 1
A buildTasks() 0 12 1
A buildFilters() 0 12 1
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
    protected function buildTasks(array $configuration)
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
    protected function buildFilters(array $configuration)
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