SyncCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
namespace AppBundle\Command;
3
4
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
/**
9
 * Main command of Sync App. Runs the directory synchronization
10
 *
11
 * @author Sergey Sadovoi <[email protected]>
12
 */
13
class SyncCommand extends ContainerAwareCommand
14
{
15
    /**
16
     * Sets name and description of the command
17
     */
18
    protected function configure()
19
    {
20
        $this
21
            ->setName('sync:run')
22
            ->setDescription('Runs the directory syncronization')
23
        ;
24
    }
25
26
    /**
27
     * Executes the current command.
28
     *
29
     * @param InputInterface  $input    Input interface
30
     * @param OutputInterface $output   Output interface
31
     *
32
     * @return int 0 if everything went fine
33
     */
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        $sync = $this->getContainer()->get('app');
37
38
        $masterFilters = $this->getFilters('master');
39
        $sync->setMasterFilters($masterFilters);
40
41
        $slaveFilters = $this->getFilters('slave');
42
        $sync->setSlaveFilters($slaveFilters);
43
44
        $sync->run();
45
    }
46
47
    /**
48
     * @param string $type  of the filters
49
     *
50
     * @return array  of the filter objects
51
     */
52
    protected function getFilters($type)
53
    {
54
        $filters   = [];
55
        $container = $this->getContainer();
56
57
        $filterConfigs = $container->getParameter($type . '.filters');
58
59
        if (!is_array($filterConfigs)) {
60
            return [];
61
        }
62
63
        foreach ($filterConfigs as $name => $options) {
64
            $filter = $container->get('filter.' . $name);
65
66
            foreach ($options as $option => $value) {
67
                $setter = 'set' . ucfirst($option);
68
                $filter->$setter($value);
69
            }
70
71
            $filters[] = $filter;
72
        }
73
74
        return $filters;
75
    }
76
}
77