Completed
Pull Request — master (#5)
by Arnaud
09:37
created

AbstractCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 122
Duplicated Lines 19.67 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 24
loc 122
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildTasks() 12 12 1
A buildFilters() 12 12 1
A loadConfigurationFile() 0 13 3
A loadConfiguration() 0 16 4
A setContainer() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 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
        $this->io->text('- Building tasks...');
51
        $builder = new TaskBuilder($this->debug);
52
53
        $tasks = $builder->build($configuration);
54
55
        $this->io->text('- Tasks build !');
56
        $this->io->newLine();
57
58
        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 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
        $this->io->text('- Building filters...');
71
        $builder = new FilterBuilder($this->eventDispatcher);
72
73
        $filters = $builder->build($configuration);
74
75
        $this->io->text('- Filters build !');
76
        $this->io->newLine();
77
78
        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
    protected function loadConfiguration(InputInterface $input)
114
    {
115
        $loader = new ConfigurationLoader();
116
117
        if ($input->hasOption('config') && $file = $input->getOption('config')) {
118
            $configuration = $loader->loadFromFile($file);
119
        } else {
120
            
121
            if (null === $this->container) {
122
                throw new Exception('Unable to find the assets configuration from the container');
123
            }
124
            $configuration = $loader->loadFromContainer($this->container);
125
        }
126
127
        return $configuration;
128
    }
129
130
    /**
131
     * Sets the container.
132
     *
133
     * @param ContainerInterface|null $container A ContainerInterface instance or null
134
     */
135
    public function setContainer(ContainerInterface $container = null)
136
    {
137
        $this->container = $container;
138
        $this->eventDispatcher = $this->container->get('event_dispatcher');
139
    }
140
}
141