Completed
Push — develop ( df26dc...23c276 )
by Paul
02:03
created

FileExecutor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 5
dl 0
loc 12
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\ExecutorException;
8
use PhpUnitGen\Executor\ExecutorInterface\ExecutorInterface;
9
use PhpUnitGen\Executor\ExecutorInterface\FileExecutorInterface;
10
use PhpUnitGen\Validator\ValidatorInterface\FileValidatorInterface;
11
use Symfony\Component\Console\Style\StyleInterface;
12
13
/**
14
 * Class FileExecutor.
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 FileExecutor implements FileExecutorInterface
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 ExecutorInterface $executor The executor for php code.
36
     */
37
    private $executor;
38
39
    /**
40
     * @var FilesystemInterface $fileSystem The file system to use.
41
     */
42
    private $fileSystem;
43
44
    /**
45
     * @var FileValidatorInterface $fileValidator The file validator to know which files we need to parse.
46
     */
47
    private $fileValidator;
48
49
    /**
50
     * DirectoryParser constructor.
51
     *
52
     * @param ConsoleConfigInterface $config        A config instance.
53
     * @param StyleInterface         $output        An output to display message.
54
     * @param ExecutorInterface      $executor      A PhpUnitGen executor.
55
     * @param FilesystemInterface    $fileSystem    A file system instance.
56
     * @param FileValidatorInterface $fileValidator A file validator.
57
     */
58
    public function __construct(
59
        ConsoleConfigInterface $config,
60
        StyleInterface $output,
61
        ExecutorInterface $executor,
62
        FilesystemInterface $fileSystem,
63
        FileValidatorInterface $fileValidator
64
    ) {
65
        $this->config        = $config;
66
        $this->output        = $output;
67
        $this->executor      = $executor;
68
        $this->fileSystem    = $fileSystem;
69
        $this->fileValidator = $fileValidator;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function execute(string $sourcePath, string $targetPath): void
76
    {
77
        if (! $this->fileValidator->validate($sourcePath)) {
78
            return;
79
        }
80
81
        $this->checkTargetPath($targetPath);
82
83
        /** @scrutinizer ignore-type Because file readability has already been checked. */
84
        $content = $this->fileSystem->read($sourcePath);
85
        $code = $this->executor->execute($content);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type false; however, parameter $code of PhpUnitGen\Executor\Exec...torInterface::execute() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

85
        $code = $this->executor->execute(/** @scrutinizer ignore-type */ $content);
Loading history...
86
87
        $this->fileSystem->write($targetPath, $code);
88
89
        // Output that a file is parsed
90
        $this->output->text(sprintf('Parsing file "%s" completed.', $sourcePath));
91
    }
92
93
    /**
94
     * Check if an old file exists. If overwrite option is activated, delete it, else, throw an exception.
95
     *
96
     * @param string $targetPath The target file path.
97
     *
98
     * @throws ExecutorException If overwrite option is deactivated and file exists.
99
     */
100
    public function checkTargetPath(string $targetPath): void
101
    {
102
        $targetPathExists = $this->fileSystem->has($targetPath);
103
104
        if ($targetPathExists && ! $this->config->hasOverwrite()) {
105
            throw new ExecutorException(sprintf('The target file "%s" already exists.', $targetPath));
106
        }
107
108
        if ($targetPathExists) {
109
            $this->fileSystem->delete($targetPath);
110
        }
111
    }
112
}
113