Completed
Push — develop ( fbe971...cec1b5 )
by Paul
02:00
created

ConsoleExecutor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 107
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A executeOnDirectories() 0 7 3
A executeOnFiles() 0 7 3
A invoke() 0 20 1
1
<?php
2
3
namespace PhpUnitGen\Executor;
4
5
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface;
6
use PhpUnitGen\Exception\Exception;
7
use PhpUnitGen\Exception\ExceptionInterface\ExceptionCatcherInterface;
8
use PhpUnitGen\Executor\ExecutorInterface\ConsoleExecutorInterface;
9
use PhpUnitGen\Executor\ExecutorInterface\DirectoryExecutorInterface;
10
use PhpUnitGen\Executor\ExecutorInterface\FileExecutorInterface;
11
use Symfony\Component\Console\Style\StyleInterface;
12
use Symfony\Component\Stopwatch\Stopwatch;
13
14
/**
15
 * Class ConsoleExecutor.
16
 *
17
 * @author     Paul Thébaud <[email protected]>.
18
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
19
 * @license    https://opensource.org/licenses/MIT The MIT license.
20
 * @link       https://github.com/paul-thebaud/phpunit-generator
21
 * @since      Class available since Release 2.0.0.
22
 */
23
class ConsoleExecutor implements ConsoleExecutorInterface
24
{
25
    /**
26
     * @var ConsoleConfigInterface $config The configuration to use.
27
     */
28
    private $config;
29
30
    /**
31
     * @var StyleInterface $output The output to display message.
32
     */
33
    private $output;
34
35
    /**
36
     * @var Stopwatch $stopwatch The stopwatch to measure duration and memory usage.
37
     */
38
    private $stopwatch;
39
40
    /**
41
     * @var DirectoryExecutorInterface $directoryExecutor A directory executor.
42
     */
43
    private $directoryExecutor;
44
45
    /**
46
     * @var FileExecutorInterface $fileExecutor A file executor.
47
     */
48
    private $fileExecutor;
49
50
    /**
51
     * @var ExceptionCatcherInterface $exceptionCatcher An exception catcher to catch exception.
52
     */
53
    private $exceptionCatcher;
54
55
    /**
56
     * ConsoleExecutor constructor.
57
     *
58
     * @param ConsoleConfigInterface     $config            The config to use.
59
     * @param StyleInterface             $output            The output to use.
60
     * @param Stopwatch                  $stopwatch         The stopwatch to use.
61
     * @param DirectoryExecutorInterface $directoryExecutor The directory executor.
62
     * @param FileExecutorInterface      $fileExecutor      The file executor.
63
     * @param ExceptionCatcherInterface  $exceptionCatcher  The exception catcher.
64
     */
65
    public function __construct(
66
        ConsoleConfigInterface $config,
67
        StyleInterface $output,
68
        Stopwatch $stopwatch,
69
        DirectoryExecutorInterface $directoryExecutor,
70
        FileExecutorInterface $fileExecutor,
71
        ExceptionCatcherInterface $exceptionCatcher
72
    ) {
73
        $this->config            = $config;
74
        $this->output            = $output;
75
        $this->stopwatch         = $stopwatch;
76
        $this->directoryExecutor = $directoryExecutor;
77
        $this->fileExecutor      = $fileExecutor;
78
        $this->exceptionCatcher  = $exceptionCatcher;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function invoke(): void
85
    {
86
        $this->executeOnDirectories();
87
88
        $this->output->section('Global files parsing begins.');
89
90
        $this->executeOnFiles();
91
92
        $event = $this->stopwatch->stop('command');
93
94
        $this->output->section('PhpUnitGen finished all tasks.');
95
        $this->output->text(sprintf(
96
            '<options=bold,underscore>Duration:</> %d milliseconds',
97
            $event->getDuration()
98
        ));
99
        $this->output->text(sprintf(
100
            '<options=bold,underscore>Memory usage:</> %d bytes',
101
            $event->getMemory()
102
        ));
103
        $this->output->newLine();
104
    }
105
106
    /**
107
     * Execute PhpUnitGen tasks on directories.
108
     */
109
    private function executeOnDirectories(): void
110
    {
111
        foreach ($this->config->getDirectories() as $source => $target) {
112
            try {
113
                $this->directoryExecutor->invoke($source, $target);
114
            } catch (Exception $exception) {
115
                $this->exceptionCatcher->catch($exception);
116
            }
117
        }
118
    }
119
120
    /**
121
     * Execute PhpUnitGen tasks on files.
122
     */
123
    private function executeOnFiles(): void
124
    {
125
        foreach ($this->config->getFiles() as $source => $target) {
126
            try {
127
                $this->fileExecutor->invoke($source, $target);
128
            } catch (Exception $exception) {
129
                $this->exceptionCatcher->catch($exception);
130
            }
131
        }
132
    }
133
}
134