Passed
Push — develop ( e84888...251d34 )
by Paul
02:54
created

FileValidator::validatePathExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Validator;
4
5
use League\Flysystem\FilesystemInterface;
6
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface;
7
use PhpUnitGen\Exception\FileNotFoundException;
8
use PhpUnitGen\Validator\ValidatorInterface\FileValidatorInterface;
9
use Respect\Validation\Validator;
10
11
/**
12
 * Class FileValidator.
13
 *
14
 * @author     Paul Thébaud <[email protected]>.
15
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
16
 * @license    https://opensource.org/licenses/MIT The MIT license.
17
 * @link       https://github.com/paul-thebaud/phpunit-generator
18
 * @since      Class available since Release 2.0.0.
19
 */
20
class FileValidator implements FileValidatorInterface
21
{
22
    /**
23
     * @var ConsoleConfigInterface $config The configuration to use.
24
     */
25
    private $config;
26
27
    /**
28
     * @var FilesystemInterface $fileSystem The file system to use.
29
     */
30
    private $fileSystem;
31
32
    /**
33
     * FileValidator constructor.
34
     *
35
     * @param ConsoleConfigInterface $config     The config to use.
36
     * @param FilesystemInterface    $filesystem The file system to use.
37
     */
38
    public function __construct(ConsoleConfigInterface $config, FilesystemInterface $filesystem)
39
    {
40
        $this->config     = $config;
41
        $this->fileSystem = $filesystem;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function validate(string $path): bool
48
    {
49
        if (($type = $this->getPathType($path)) === null) {
50
            throw new FileNotFoundException(sprintf('The source file "%s" does not exist.', $path));
51
        }
52
        if ($type === 'dir') {
53
            return false;
54
        }
55
        if (! $this->validateIncludeRegex($path)
56
            || ! $this->validateExcludeRegex($path)
57
        ) {
58
            return false;
59
        }
60
61
        return true;
62
    }
63
64
    /**
65
     * Validate file has a valid path.
66
     *
67
     * @param string $path The file path.
68
     *
69
     * @return string|null "file" if its a file, "dir" if its a dir and null if the path does not exists.
70
     */
71
    private function getPathType(string $path): ?string
72
    {
73
        return $this->fileSystem->has($path) && $this->fileSystem->get($path)->getType() === 'file';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fileSystem...)->getType() === 'file' returns the type boolean which is incompatible with the type-hinted return null|string.
Loading history...
74
    }
75
76
    /**
77
     * Validate file has the include regex if this regex is set.
78
     *
79
     * @param string $path The file path.
80
     *
81
     * @return bool True if it pass this validation.
82
     */
83
    private function validateIncludeRegex(string $path): bool
84
    {
85
        $includeRegex = $this->config->getIncludeRegex();
86
        return $includeRegex === null || Validator::regex($includeRegex)->validate($path) === true;
87
    }
88
89
    /**
90
     * Validate file has not the exclude regex if this regex is set.
91
     *
92
     * @param string $path The file path.
93
     *
94
     * @return bool True if it pass this validation.
95
     */
96
    private function validateExcludeRegex(string $path): bool
97
    {
98
        $excludeRegex = $this->config->getExcludeRegex();
99
        return $excludeRegex === null || Validator::regex($excludeRegex)->validate($path) !== true;
100
    }
101
}
102