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

DirectoryExecutor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A invoke() 0 12 4
A executeFileExecutor() 0 14 3
1
<?php
2
3
namespace PhpUnitGen\Executor;
4
5
use League\Flysystem\FilesystemInterface;
6
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface;
7
use PhpUnitGen\Exception\Exception;
8
use PhpUnitGen\Exception\ExceptionInterface\ExceptionCatcherInterface;
9
use PhpUnitGen\Exception\ExecutorException;
10
use PhpUnitGen\Executor\ExecutorInterface\DirectoryExecutorInterface;
11
use PhpUnitGen\Executor\ExecutorInterface\FileExecutorInterface;
12
use PhpUnitGen\Report\ReportInterface\ReportInterface;
13
use Symfony\Component\Console\Style\StyleInterface;
14
15
/**
16
 * Class DirectoryExecutor.
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 DirectoryExecutor implements DirectoryExecutorInterface
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 FileExecutorInterface $fileExecutor The file executor to parse files.
38
     */
39
    private $fileExecutor;
40
41
    /**
42
     * @var FilesystemInterface $fileSystem The file system to use.
43
     */
44
    private $fileSystem;
45
46
    /**
47
     * @var ExceptionCatcherInterface $exceptionCatcher An exception catcher to catch exception.
48
     */
49
    private $exceptionCatcher;
50
51
    /**
52
     * @var ReportInterface $report The report to use.
53
     */
54
    private $report;
55
56
    /**
57
     * DirectoryParser constructor.
58
     *
59
     * @param ConsoleConfigInterface    $config           A config instance.
60
     * @param StyleInterface            $output           An output to display message.
61
     * @param FileExecutorInterface     $fileExecutor     A file executor.
62
     * @param FilesystemInterface       $fileSystem       A file system instance.
63
     * @param ExceptionCatcherInterface $exceptionCatcher The exception catcher.
64
     * @param ReportInterface           $report           The report.
65
     */
66
    public function __construct(
67
        ConsoleConfigInterface $config,
68
        StyleInterface $output,
69
        FileExecutorInterface $fileExecutor,
70
        FilesystemInterface $fileSystem,
71
        ExceptionCatcherInterface $exceptionCatcher,
72
        ReportInterface $report
73
    ) {
74
        $this->config           = $config;
75
        $this->output           = $output;
76
        $this->fileExecutor     = $fileExecutor;
77
        $this->fileSystem       = $fileSystem;
78
        $this->exceptionCatcher = $exceptionCatcher;
79
        $this->report           = $report;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function invoke(string $sourcePath, string $targetPath): void
86
    {
87
        $this->output->section(sprintf('Directory "%s" parsing begins.', $sourcePath));
88
89
        // Check if source directory exists
90
        if (! $this->fileSystem->has($sourcePath) || ! $this->fileSystem->getMimetype($sourcePath) === 'directory') {
91
            throw new ExecutorException(sprintf('The source directory "%s" does not exist.', $sourcePath));
92
        }
93
94
        // List content of directory
95
        foreach ($this->fileSystem->listContents($sourcePath, true) as $file) {
96
            $this->executeFileExecutor($sourcePath, $targetPath, $file['path']);
97
        }
98
    }
99
100
    /**
101
     * Execute the file executor for a file in a directory.
102
     *
103
     * @param string $sourcePath The directory source path.
104
     * @param string $targetPath The directory target path.
105
     * @param string $filePath   The file path.
106
     *
107
     * @throws ExecutorException If there was an error during the process.
108
     */
109
    private function executeFileExecutor(string $sourcePath, string $targetPath, string $filePath): void
110
    {
111
        try {
112
            $name = pathinfo($filePath)['filename'] . 'Test';
113
            $targetPath = str_replace($sourcePath, $targetPath, $filePath);
114
            $targetPath = str_replace('.php', 'Test.php', $targetPath);
115
            // Execute file executor
116
            $result = $this->fileExecutor->invoke($filePath, $targetPath, $name);
117
118
            if ($result === true) {
119
                $this->report->increaseParsedFileFromDirectoryNumber();
120
            }
121
        } catch (Exception $exception) {
122
            $this->exceptionCatcher->catch($exception, $sourcePath);
123
        }
124
    }
125
}
126