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
|
|
|
|| ! $this->validateIncludeRegex($path) |
52
|
|
|
|| ! $this->validateExcludeRegex($path) |
53
|
|
|
) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Not readable file |
58
|
|
|
if ($this->fileSystem->getVisibility($path) === AdapterInterface::VISIBILITY_PRIVATE) { |
59
|
|
|
throw new NotReadableFileException(sprintf('The file "%s" is not readable.', $path)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Validate file has a valid path. |
67
|
|
|
* |
68
|
|
|
* @param string $path The file path. |
69
|
|
|
* |
70
|
|
|
* @return bool True if it pass this validation. |
71
|
|
|
*/ |
72
|
|
|
private function validatePath(string $path): bool |
73
|
|
|
{ |
74
|
|
|
return $this->fileSystem->has($path) && $this->fileSystem->get($path)->getType() === 'file'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Validate file has the include regex if this regex is set. |
79
|
|
|
* |
80
|
|
|
* @param string $path The file path. |
81
|
|
|
* |
82
|
|
|
* @return bool True if it pass this validation. |
83
|
|
|
*/ |
84
|
|
|
private function validateIncludeRegex(string $path): bool |
85
|
|
|
{ |
86
|
|
|
$includeRegex = $this->config->getIncludeRegex(); |
87
|
|
|
return $includeRegex === null || Validator::regex($includeRegex)->validate($path) === true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Validate file has not the exclude regex if this regex is set. |
92
|
|
|
* |
93
|
|
|
* @param string $path The file path. |
94
|
|
|
* |
95
|
|
|
* @return bool True if it pass this validation. |
96
|
|
|
*/ |
97
|
|
|
private function validateExcludeRegex(string $path): bool |
98
|
|
|
{ |
99
|
|
|
$excludeRegex = $this->config->getExcludeRegex(); |
100
|
|
|
return $excludeRegex === null || Validator::regex($excludeRegex)->validate($path) !== true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|