Completed
Push — develop ( 1b34c6...481302 )
by Paul
02:47
created

ConsoleExecutor::executeOnFiles()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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