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

DirectoryExecutor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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\ExecutorException;
9
use PhpUnitGen\Executor\ExecutorInterface\DirectoryExecutorInterface;
10
use PhpUnitGen\Executor\ExecutorInterface\FileExecutorInterface;
11
use Symfony\Component\Console\Style\StyleInterface;
12
13
/**
14
 * Class DirectoryExecutor.
15
 *
16
 * @author     Paul Thébaud <[email protected]>.
17
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
18
 * @license    https://opensource.org/licenses/MIT The MIT license.
19
 * @link       https://github.com/paul-thebaud/phpunit-generator
20
 * @since      Class available since Release 2.0.0.
21
 */
22
class DirectoryExecutor implements DirectoryExecutorInterface
23
{
24
    /**
25
     * @var ConsoleConfigInterface $config The configuration to use.
26
     */
27
    private $config;
28
29
    /**
30
     * @var StyleInterface $output The output to display message.
31
     */
32
    private $output;
33
34
    /**
35
     * @var FileExecutorInterface $fileExecutor The file executor to parse files.
36
     */
37
    private $fileExecutor;
38
39
    /**
40
     * @var FilesystemInterface $fileSystem The file system to use.
41
     */
42
    private $fileSystem;
43
44
    /**
45
     * DirectoryParser constructor.
46
     *
47
     * @param ConsoleConfigInterface $config       A config instance.
48
     * @param StyleInterface         $output       An output to display message.
49
     * @param FileExecutorInterface  $fileExecutor A file executor.
50
     * @param FilesystemInterface    $fileSystem   A file system instance.
51
     */
52
    public function __construct(
53
        ConsoleConfigInterface $config,
54
        StyleInterface $output,
55
        FileExecutorInterface $fileExecutor,
56
        FilesystemInterface $fileSystem
57
    ) {
58
        $this->config       = $config;
59
        $this->output       = $output;
60
        $this->fileExecutor = $fileExecutor;
61
        $this->fileSystem   = $fileSystem;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function invoke(string $sourcePath, string $targetPath): void
68
    {
69
        $this->output->section(sprintf('Directory "%s" parsing begins.', $sourcePath));
70
71
        // Check if source directory exists
72
        if (! $this->fileSystem->has($sourcePath) || ! $this->fileSystem->getMimetype($sourcePath) === 'directory') {
73
            throw new ExecutorException(sprintf('The source directory "%s" does not exist.', $sourcePath));
74
        }
75
76
        // List content of directory
77
        foreach ($this->fileSystem->listContents($sourcePath, true) as $file) {
78
            $this->executeFileExecutor($sourcePath, $targetPath, $file['path']);
79
        }
80
    }
81
82
    /**
83
     * Execute the file executor for a file in a directory.
84
     *
85
     * @param string $sourcePath The directory source path.
86
     * @param string $targetPath The directory target path.
87
     * @param string $filePath   The file path.
88
     *
89
     * @throws ExecutorException If there was an error during the process.
90
     */
91
    private function executeFileExecutor(string $sourcePath, string $targetPath, string $filePath): void
92
    {
93
        try {
94
            // Execute file executor
95
            $this->fileExecutor->invoke($filePath, str_replace($sourcePath, $targetPath, $filePath));
96
        } catch (Exception $exception) {
97
            if ($this->config->hasIgnore()) {
98
                $this->output->note($exception->getMessage());
99
            } else {
100
                throw new ExecutorException($exception->getMessage());
101
            }
102
        }
103
    }
104
}
105