Completed
Push — master ( 45600c...8f79c4 )
by Paul
10:17 queued 06:57
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
/**
4
 * This file is part of PHPUnit Generator.
5
 *
6
 * (c) 2017-2018 Paul Thébaud <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhpUnitGen\Validator;
13
14
use League\Flysystem\FilesystemInterface;
15
use PhpUnitGen\Configuration\ConfigurationInterface\ConsoleConfigInterface;
16
use PhpUnitGen\Exception\FileNotFoundException;
17
use PhpUnitGen\Validator\ValidatorInterface\FileValidatorInterface;
18
use Respect\Validation\Validator;
19
20
/**
21
 * Class FileValidator.
22
 *
23
 * @author     Paul Thébaud <[email protected]>.
24
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
25
 * @license    https://opensource.org/licenses/MIT The MIT license.
26
 * @link       https://github.com/paul-thebaud/phpunit-generator
27
 * @since      Class available since Release 2.0.0.
28
 */
29
class FileValidator implements FileValidatorInterface
30
{
31
    /**
32
     * @var ConsoleConfigInterface $config The configuration to use.
33
     */
34
    private $config;
35
36
    /**
37
     * @var FilesystemInterface $fileSystem The file system to use.
38
     */
39
    private $fileSystem;
40
41
    /**
42
     * FileValidator constructor.
43
     *
44
     * @param ConsoleConfigInterface $config     The config to use.
45
     * @param FilesystemInterface    $filesystem The file system to use.
46
     */
47
    public function __construct(ConsoleConfigInterface $config, FilesystemInterface $filesystem)
48
    {
49
        $this->config     = $config;
50
        $this->fileSystem = $filesystem;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function validate(string $path): bool
57
    {
58
        if (! $this->validatePath($path)) {
59
            return false;
60
        }
61
        if (! $this->validateIncludeRegex($path)
62
            || ! $this->validateExcludeRegex($path)
63
        ) {
64
            return false;
65
        }
66
67
        return true;
68
    }
69
70
    /**
71
     * Validate file has a valid path.
72
     *
73
     * @param string $path The file path.
74
     *
75
     * @return bool True if the path exists and is a file.
76
     *
77
     * @throws FileNotFoundException If the path corresponds to a dir.
78
     */
79
    private function validatePath(string $path): bool
80
    {
81
        if (! $this->fileSystem->has($path)) {
82
            throw new FileNotFoundException(sprintf('The source file "%s" does not exist.', $path));
83
        }
84
        if ($this->fileSystem->get($path)->getType() === 'dir') {
85
            return false;
86
        }
87
        return true;
88
    }
89
90
    /**
91
     * Validate file has the include 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 validateIncludeRegex(string $path): bool
98
    {
99
        $includeRegex = $this->config->getIncludeRegex();
100
        return $includeRegex === null || Validator::regex($includeRegex)->validate($path) === true;
101
    }
102
103
    /**
104
     * Validate file has not the exclude regex if this regex is set.
105
     *
106
     * @param string $path The file path.
107
     *
108
     * @return bool True if it pass this validation.
109
     */
110
    private function validateExcludeRegex(string $path): bool
111
    {
112
        $excludeRegex = $this->config->getExcludeRegex();
113
        return $excludeRegex === null || Validator::regex($excludeRegex)->validate($path) !== true;
114
    }
115
}
116