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->fileSystem->has($path) |
51
|
|
|
|| $this->fileSystem->get($path)->getType() !== 'file') { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Nullable regex |
56
|
|
|
$includeRegex = $this->config->getIncludeRegex(); |
57
|
|
|
$excludeRegex = $this->config->getExcludeRegex(); |
58
|
|
|
if (($includeRegex !== null && $excludeRegex !== null) |
59
|
|
|
&& ( |
60
|
|
|
Validator::regex($includeRegex)->validate($path) !== true |
61
|
|
|
|| Validator::regex($excludeRegex)->validate($path) === true |
62
|
|
|
) |
63
|
|
|
) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Not readable file |
68
|
|
|
if ($this->fileSystem->getVisibility($path) === AdapterInterface::VISIBILITY_PRIVATE) { |
69
|
|
|
throw new NotReadableFileException(sprintf('The file "%s" is not readable.', $path)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|