ConsoleExecutor::invoke()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 38
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 24
nc 4
nop 0
dl 0
loc 38
rs 9.536
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of PhpUnitGen.
5
 *
6
 * (c) 2017-2018 Paul Thébaud <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhpUnitGen\Executor;
13
14
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface;
15
use PhpUnitGen\Console\GenerateCommand;
16
use PhpUnitGen\Exception\Exception;
17
use PhpUnitGen\Exception\ExceptionInterface\ExceptionCatcherInterface;
18
use PhpUnitGen\Executor\ExecutorInterface\ConsoleExecutorInterface;
19
use PhpUnitGen\Executor\ExecutorInterface\DirectoryExecutorInterface;
20
use PhpUnitGen\Executor\ExecutorInterface\FileExecutorInterface;
21
use PhpUnitGen\Report\ReportInterface\ReportInterface;
22
use Symfony\Component\Console\Style\StyleInterface;
23
use Symfony\Component\Stopwatch\Stopwatch;
24
25
/**
26
 * Class ConsoleExecutor.
27
 *
28
 * @author     Paul Thébaud <[email protected]>.
29
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
30
 * @license    https://opensource.org/licenses/MIT The MIT license.
31
 * @link       https://github.com/paul-thebaud/phpunit-generator
32
 * @since      Class available since Release 2.0.0.
33
 */
34
class ConsoleExecutor implements ConsoleExecutorInterface
35
{
36
    /**
37
     * @var ConsoleConfigInterface $config The configuration to use.
38
     */
39
    private $config;
40
41
    /**
42
     * @var StyleInterface $output The output to display message.
43
     */
44
    private $output;
45
46
    /**
47
     * @var Stopwatch $stopwatch The stopwatch to measure duration and memory usage.
48
     */
49
    private $stopwatch;
50
51
    /**
52
     * @var DirectoryExecutorInterface $directoryExecutor A directory executor.
53
     */
54
    private $directoryExecutor;
55
56
    /**
57
     * @var FileExecutorInterface $fileExecutor A file executor.
58
     */
59
    private $fileExecutor;
60
61
    /**
62
     * @var ExceptionCatcherInterface $exceptionCatcher An exception catcher to catch exception.
63
     */
64
    private $exceptionCatcher;
65
66
    /**
67
     * @var ReportInterface $report The report to use.
68
     */
69
    private $report;
70
71
    /**
72
     * ConsoleExecutor constructor.
73
     *
74
     * @param ConsoleConfigInterface     $config            The config to use.
75
     * @param StyleInterface             $output            The output to use.
76
     * @param Stopwatch                  $stopwatch         The stopwatch to use.
77
     * @param DirectoryExecutorInterface $directoryExecutor The directory executor.
78
     * @param FileExecutorInterface      $fileExecutor      The file executor.
79
     * @param ExceptionCatcherInterface  $exceptionCatcher  The exception catcher.
80
     * @param ReportInterface            $report            The report.
81
     */
82
    public function __construct(
83
        ConsoleConfigInterface $config,
84
        StyleInterface $output,
85
        Stopwatch $stopwatch,
86
        DirectoryExecutorInterface $directoryExecutor,
87
        FileExecutorInterface $fileExecutor,
88
        ExceptionCatcherInterface $exceptionCatcher,
89
        ReportInterface $report
90
    ) {
91
        $this->config            = $config;
92
        $this->output            = $output;
93
        $this->stopwatch         = $stopwatch;
94
        $this->directoryExecutor = $directoryExecutor;
95
        $this->fileExecutor      = $fileExecutor;
96
        $this->exceptionCatcher  = $exceptionCatcher;
97
        $this->report            = $report;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function invoke(): void
104
    {
105
        if (count($this->config->getDirectories()) > 0) {
106
            $this->output->section('Directories parsing begins.');
107
108
            $this->executeOnDirectories();
109
        }
110
111
        if (count($this->config->getFiles()) > 0) {
112
            $this->output->section('Global files parsing begins.');
113
114
            $this->executeOnFiles();
115
        }
116
117
        $event = $this->stopwatch->stop(GenerateCommand::STOPWATCH_EVENT);
118
119
        $this->output->section('PhpUnitGen finished all tasks.');
120
        $this->output->text(sprintf(
121
            '<options=bold,underscore>Duration:</> %d milliseconds',
122
            $event->getDuration()
123
        ));
124
        $this->output->text(sprintf(
125
            '<options=bold,underscore>Memory usage:</> %d bytes',
126
            $event->getMemory()
127
        ));
128
        $this->output->text(sprintf(
129
            '<options=bold,underscore>Parsed files number:</> %d files',
130
            $this->report->getParsedFileNumber()
131
        ));
132
        $this->output->text(sprintf(
133
            '<options=bold,underscore>Parsed directories number:</> %d directories',
134
            $this->report->getParsedDirectoryNumber()
135
        ));
136
        $this->output->text(sprintf(
137
            '<options=bold,underscore>Errors during process:</> %d errors',
138
            $this->report->getIgnoredErrorNumber()
139
        ));
140
        $this->output->newLine();
141
    }
142
143
    /**
144
     * Execute PhpUnitGen tasks on directories.
145
     *
146
     * @throws Exception If an error occurred during process.
147
     */
148
    private function executeOnDirectories(): void
149
    {
150
        foreach ($this->config->getDirectories() as $source => $target) {
151
            try {
152
                $this->directoryExecutor->invoke($source, $target);
153
                $this->report->increaseParsedDirectoryNumber();
154
            } catch (Exception $exception) {
155
                $this->exceptionCatcher->catch($exception, $source);
156
            }
157
        }
158
    }
159
160
    /**
161
     * Execute PhpUnitGen tasks on files.
162
     *
163
     * @throws Exception If an error occurred during process.
164
     */
165
    private function executeOnFiles(): void
166
    {
167
        foreach ($this->config->getFiles() as $source => $target) {
168
            try {
169
                $name = pathinfo($target)['filename'];
170
                $this->fileExecutor->invoke($source, $target, $name);
171
            } catch (Exception $exception) {
172
                $this->exceptionCatcher->catch($exception, $source);
173
            }
174
        }
175
    }
176
}
177