Passed
Push — develop ( 481302...fbb092 )
by Paul
02:15
created

FileExecutor::invoke()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 3
dl 0
loc 27
rs 8.5806
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\Exception\NotReadableFileException;
9
use PhpUnitGen\Executor\ExecutorInterface\ExecutorInterface;
10
use PhpUnitGen\Executor\ExecutorInterface\FileExecutorInterface;
11
use PhpUnitGen\Report\ReportInterface\ReportInterface;
12
use PhpUnitGen\Validator\ValidatorInterface\FileValidatorInterface;
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 invoke(string $sourcePath, string $targetPath, string $name = 'GeneratedTest'): bool
78
    {
79
        if (! $this->fileValidator->validate($sourcePath)) {
80
            return false;
81
        }
82
83
        $content = $this->fileSystem->read($sourcePath);
84
85
        if ($content === false) {
86
            throw new NotReadableFileException(sprintf('The file "%s" is not readable.', $sourcePath));
87
        }
88
89
        // We ignore the type checked because we already check the readability
90
        $code = $this->executor->invoke($content, $name);
91
92
        if ($code === null) {
93
            return false;
94
        }
95
96
        $this->checkTargetPath($targetPath);
97
98
        $this->fileSystem->write($targetPath, $code);
99
100
        // Output that a file is parsed
101
        $this->output->text(sprintf('Parsing file "%s" completed.', $sourcePath));
102
103
        return true;
104
    }
105
106
    /**
107
     * Check if an old file exists. If overwrite option is activated, delete it, else, throw an exception.
108
     *
109
     * @param string $targetPath The target file path.
110
     *
111
     * @throws ExecutorException If overwrite option is deactivated and file exists.
112
     */
113
    public function checkTargetPath(string $targetPath): void
114
    {
115
        $targetPathExists = $this->fileSystem->has($targetPath);
116
117
        if ($targetPathExists && ! $this->config->hasOverwrite()) {
118
            throw new ExecutorException(sprintf('The target file "%s" already exists.', $targetPath));
119
        }
120
121
        if ($targetPathExists) {
122
            $this->fileSystem->delete($targetPath);
123
        }
124
    }
125
}
126