Completed
Push — develop ( a0b2b6...96900c )
by Paul
02:40
created

FileExecutor::execute()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 4
nop 2
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Executor;
4
5
use League\Flysystem\AdapterInterface;
6
use League\Flysystem\FilesystemInterface;
7
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface;
8
use PhpUnitGen\Exception\ExecutorException;
9
use PhpUnitGen\Executor\ExecutorInterface\ExecutorInterface;
10
use PhpUnitGen\Executor\ExecutorInterface\FileExecutorInterface;
11
use PhpUnitGen\Validator\ValidatorInterface\FileValidatorInterface;
12
use Respect\Validation\Validator;
13
use Symfony\Component\Console\Style\StyleInterface;
14
15
/**
16
 * Class FileExecutor.
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 FileExecutor implements FileExecutorInterface
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 ExecutorInterface $executor The executor for php code.
38
     */
39
    private $executor;
40
41
    /**
42
     * @var FilesystemInterface $fileSystem The file system to use.
43
     */
44
    private $fileSystem;
45
46
    /**
47
     * @var FileValidatorInterface $fileValidator The file validator to know which files we need to parse.
48
     */
49
    private $fileValidator;
50
51
    /**
52
     * DirectoryParser constructor.
53
     *
54
     * @param ConsoleConfigInterface $config        A config instance.
55
     * @param StyleInterface         $output        An output to display message.
56
     * @param ExecutorInterface      $executor      A PhpUnitGen executor.
57
     * @param FilesystemInterface    $fileSystem    A file system instance.
58
     * @param FileValidatorInterface $fileValidator A file validator.
59
     */
60
    public function __construct(
61
        ConsoleConfigInterface $config,
62
        StyleInterface $output,
63
        ExecutorInterface $executor,
64
        FilesystemInterface $fileSystem,
65
        FileValidatorInterface $fileValidator
66
    ) {
67
        $this->config        = $config;
68
        $this->output        = $output;
69
        $this->executor      = $executor;
70
        $this->fileSystem    = $fileSystem;
71
        $this->fileValidator = $fileValidator;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function execute(string $sourcePath, string $targetPath): void
78
    {
79
        if (! $this->fileValidator->validate($sourcePath)) {
80
            return;
81
        }
82
83
        $targetPathExists = $this->fileSystem->has($targetPath);
84
85
        if ($targetPathExists && ! $this->config->hasOverwrite()) {
86
            throw new ExecutorException(sprintf('The target file "%s" already exists.', $targetPath));
87
        }
88
89
        $code = $this->executor->execute($this->fileSystem->read($sourcePath));
0 ignored issues
show
Bug introduced by
It seems like $this->fileSystem->read($sourcePath) 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

89
        $code = $this->executor->execute(/** @scrutinizer ignore-type */ $this->fileSystem->read($sourcePath));
Loading history...
90
91
        if ($targetPathExists) {
92
            $this->fileSystem->delete($targetPath);
93
        }
94
        $this->fileSystem->write($targetPath, $code);
95
96
        // Output that a file is parsed
97
        $this->output->text(sprintf('Parsing file "%s" completed.', $sourcePath));
98
    }
99
}