WatchCommand::execute()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 0
cts 44
cp 0
rs 8.7636
c 0
b 0
f 0
cc 4
nc 6
nop 2
crap 20

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace JK\SamBundle\Command;
4
5
use Exception;
6
use JK\SamBundle\Watcher\Indexer\FileIndexer;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
12
class WatchCommand extends AbstractCommand
13
{
14
    /**
15
     * Indicate if the watching loop should continue.
16
     *
17
     * @var bool
18
     */
19
    protected $shouldStop = false;
20
21
    /**
22
     *
23
     */
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('jk:assets:watch')
28
            ->setDescription('Watch the assets according to your tasks configuration ("jk_assets")')
29
        ;
30
    }
31
32
    /**
33
     * @param InputInterface $input
34
     * @param OutputInterface $output
35
     * @return int|null|void
36
     * @throws Exception
37
     */
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $this->io = new SymfonyStyle($input, $output);
41
        $this
42
            ->io
43
            ->title('Symfony PHP Assets Manager');
44
        $this
45
            ->io
46
            ->section('Watch assets changes');
47
48
        $configuration = $this->loadConfiguration($input);
49
50
        // get debug mode
51
        $this->debug = $configuration['debug'];
52
53
        if ($this->debug) {
54
            $this->io->note('Debug Mode...');
55
        }
56
57
        // on Ctrl+C, we must stop to watch files
58
        pcntl_signal(SIGTERM, [$this, 'stopWatch']);
59
        pcntl_signal(SIGINT, [$this, 'stopWatch']);
60
61
        $indexer = new FileIndexer();
62
        $sources = $this->collectSources($configuration['tasks']);
63
64
        // start watching sources
65
        $runCommand = new BuildCommand();
66
        $runCommand->setContainer($this->container);
67
        $runCommand->run(new ArrayInput([]), $output);
68
        
69
        $this
70
            ->io
71
            ->text('Watching...');
72
73
        while (!$this->shouldStop) {
74
            $indexer->index($sources);
75
76
            if ($indexer->hasChangedEntries()) {
77
                $this
78
                    ->io
79
                    ->note('Sources has been modified...');
80
81
                $runCommand = new BuildCommand();
82
                $runCommand->setContainer($this->container);
83
                $runCommand->run(new ArrayInput([]), $output);
84
    
85
                // re-index the sources because they could have been changed during the previous build (for example,
86
                // if the destination of a task is the source of an other task, it could lead to infinite build)
87
                $indexer->index($sources);
88
89
                $this
90
                    ->io
91
                    ->text('Watching...');
92
            }
93
94
            pcntl_signal_dispatch();
95
            sleep(1);
96
        }
97
98
        // display end message
99
        $this->io->success('Assets watching end');
100
101
        return;
102
    }
103
104
    public function stopWatch()
105
    {
106
        $this->shouldStop = true;
107
        $this
108
            ->io
109
            ->note('Stop watching changes');
110
    }
111
112
    /**
113
     * @param array $tasks
114
     *
115
     * @return array
116
     */
117
    protected function collectSources(array $tasks)
118
    {
119
        $sources = [];
120
        $directories = [];
121
        $tasks = $this->buildTasks($tasks);
122
123
        foreach ($tasks as $task) {
124
            $sources = array_merge($sources, $task->getSources());
125
        }
126
        foreach ($sources as $source) {
127
128
            if (is_dir($source)) {
129
                $directory = $source;
130
            } else {
131
                $directory = dirname($source);
132
            }
133
            $this
134
                ->io
135
                ->text('Found new directory : '.$directory);
136
            $directories[] = $directory;
137
        }
138
139
        return $directories;
140
    }
141
}
142