Passed
Push — develop ( ea766e...fdb6a9 )
by Paul
01:50
created

FileValidator::validateIncludeRegex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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