ClassExtendsValidator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 43
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 20 3
1
<?php
2
/*
3
 * 2018 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Configuration Object project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\ConfigurationObject\Validation\Validator;
15
16
use Romm\ConfigurationObject\Core\Core;
17
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
18
19
class ClassExtendsValidator extends AbstractValidator
20
{
21
    const ERROR_CODE_CLASS_NOT_FOUND = 1487711134;
22
    const ERROR_CODE_CLASS_NOT_VALID = 1487711239;
23
24
    /**
25
     * @var array
26
     */
27
    protected $supportedOptions = [
28
        'class' => [
29
            '',
30
            'Name of the class that the original class should extend.',
31
            'string',
32
            true
33
        ]
34
    ];
35
36
    /**
37
     * Checks if the value is an existing class.
38
     *
39
     * @param mixed $value The value that should be validated.
40
     */
41
    public function isValid($value)
42
    {
43
        if (false === Core::get()->classExists($value)) {
44
            $errorMessage = $this->translateErrorMessage(
45
                'validator.class_exists.not_valid',
46
                'configuration_object',
47
                [$value]
48
            );
49
50
            $this->addError($errorMessage, self::ERROR_CODE_CLASS_NOT_FOUND);
51
        } elseif (false === in_array($this->options['class'], class_parents($value))) {
52
            $errorMessage = $this->translateErrorMessage(
53
                'validator.class_extends_class.not_valid',
54
                'configuration_object',
55
                [$value, $this->options['class']]
56
            );
57
58
            $this->addError($errorMessage, self::ERROR_CODE_CLASS_NOT_VALID);
59
        }
60
    }
61
}
62