DirectoryExecutor   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 102
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A invoke() 0 19 4
A __construct() 0 14 1
A executeFileExecutor() 0 10 2
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 League\Flysystem\FilesystemInterface;
15
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface;
16
use PhpUnitGen\Exception\Exception;
17
use PhpUnitGen\Exception\ExceptionInterface\ExceptionCatcherInterface;
18
use PhpUnitGen\Exception\FileNotFoundException;
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
24
/**
25
 * Class DirectoryExecutor.
26
 *
27
 * @author     Paul Thébaud <[email protected]>.
28
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
29
 * @license    https://opensource.org/licenses/MIT The MIT license.
30
 * @link       https://github.com/paul-thebaud/phpunit-generator
31
 * @since      Class available since Release 2.0.0.
32
 */
33
class DirectoryExecutor implements DirectoryExecutorInterface
34
{
35
    /**
36
     * @var ConsoleConfigInterface $config The configuration to use.
37
     */
38
    private $config;
39
40
    /**
41
     * @var StyleInterface $output The output to display message.
42
     */
43
    private $output;
44
45
    /**
46
     * @var FileExecutorInterface $fileExecutor The file executor to parse files.
47
     */
48
    private $fileExecutor;
49
50
    /**
51
     * @var FilesystemInterface $fileSystem The file system to use.
52
     */
53
    private $fileSystem;
54
55
    /**
56
     * @var ExceptionCatcherInterface $exceptionCatcher An exception catcher to catch exception.
57
     */
58
    private $exceptionCatcher;
59
60
    /**
61
     * @var ReportInterface $report The report to use.
62
     */
63
    private $report;
64
65
    /**
66
     * DirectoryParser constructor.
67
     *
68
     * @param ConsoleConfigInterface    $config           A config instance.
69
     * @param StyleInterface            $output           An output to display message.
70
     * @param FileExecutorInterface     $fileExecutor     A file executor.
71
     * @param FilesystemInterface       $fileSystem       A file system instance.
72
     * @param ExceptionCatcherInterface $exceptionCatcher The exception catcher.
73
     * @param ReportInterface           $report           The report.
74
     */
75
    public function __construct(
76
        ConsoleConfigInterface $config,
77
        StyleInterface $output,
78
        FileExecutorInterface $fileExecutor,
79
        FilesystemInterface $fileSystem,
80
        ExceptionCatcherInterface $exceptionCatcher,
81
        ReportInterface $report
82
    ) {
83
        $this->config           = $config;
84
        $this->output           = $output;
85
        $this->fileExecutor     = $fileExecutor;
86
        $this->fileSystem       = $fileSystem;
87
        $this->exceptionCatcher = $exceptionCatcher;
88
        $this->report           = $report;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function invoke(string $sourcePath, string $targetPath): void
95
    {
96
        // Remove last '/' from paths
97
        $sourcePath = preg_replace('#/+$#', '', $sourcePath);
98
        $targetPath = preg_replace('#/+$#', '', $targetPath);
99
100
        $this->output->section(sprintf('Directory "%s" parsing begins.', $sourcePath));
101
102
        // Check if source directory exists
103
        if (! $this->fileSystem->has($sourcePath)) {
104
            throw new FileNotFoundException(sprintf('The source directory "%s" does not exist', $sourcePath));
105
        }
106
        if ($this->fileSystem->get($sourcePath)->getType() !== 'dir') {
0 ignored issues
show
Deprecated Code introduced by
The function League\Flysystem\FilesystemInterface::get() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

106
        if (/** @scrutinizer ignore-deprecated */ $this->fileSystem->get($sourcePath)->getType() !== 'dir') {
Loading history...
107
            throw new FileNotFoundException(sprintf('The source path "%s" is not a directory', $sourcePath));
108
        }
109
110
        // List content of directory
111
        foreach ($this->fileSystem->listContents($sourcePath, true) as $file) {
112
            $this->executeFileExecutor($sourcePath, $targetPath, $file['path']);
113
        }
114
    }
115
116
    /**
117
     * Execute the file executor for a file in a directory.
118
     *
119
     * @param string $sourcePath The directory source path.
120
     * @param string $targetPath The directory target path.
121
     * @param string $filePath   The file path.
122
     *
123
     * @throws Exception If there was an error during the process.
124
     */
125
    private function executeFileExecutor(string $sourcePath, string $targetPath, string $filePath): void
126
    {
127
        try {
128
            $name       = pathinfo($filePath)['filename'] . 'Test';
129
            $targetPath = str_replace($sourcePath, $targetPath, $filePath);
130
            $targetPath = preg_replace('/(.php|.phtml)/', 'Test.php', $targetPath);
131
            // Execute file executor
132
            $this->fileExecutor->invoke($filePath, $targetPath, $name);
133
        } catch (Exception $exception) {
134
            $this->exceptionCatcher->catch($exception, $filePath);
135
        }
136
    }
137
}
138