Completed
Push — master ( ad6a3c...062ce5 )
by Jonathan
03:32
created

StaticValidator::execute()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
ccs 15
cts 15
cp 1
rs 8.439
cc 5
eloc 14
nc 4
nop 3
crap 5
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2016 Text Control GmbH
12
 */
13
namespace TxTextControl\ReportingCloud\Validator;
14
15
use Zend\Validator\StaticValidator as StaticValidatorValidatorZend;
16
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
17
use TxTextControl\ReportingCloud\PluginManagerTrait;
18
19
/**
20
 * StaticValidator validator
21
 *
22
 * @package TxTextControl\ReportingCloud
23
 * @author  Jonathan Maron (@JonathanMaron)
24
 */
25
class StaticValidator extends StaticValidatorValidatorZend
26
{
27
    /**
28
     * ReportingCloud Validator classes
29
     *
30
     * @var array
31
     */
32
    static protected $invokableClasses = [
33
                        DateTime::class,
34
                        DocumentExtension::class,
35
                        FileExists::class,
36
                        FileExtension::class,
37
                        FileHasExtension::class,
38
                        ImageFormat::class,
39
                        Page::class,
40
                        ReturnFormat::class,
41
                        TemplateExtension::class,
42
                        TemplateFormat::class,
43
                        TemplateName::class,
44
                        Timestamp::class,
45
                        TypeArray::class,
46
                        TypeBoolean::class,
47
                        TypeInteger::class,
48
                        TypeString::class,
49
                        ZoomFactor::class,
50
    ];
51
52
    /**
53
     * Get plugin manager for loading validator classes, adding ReportingCloud Validator classes
54
     *
55
     * @return \Zend\Validator\ValidatorPluginManager
56
     */
57 152 View Code Duplication
    public static function getPluginManager()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59 152
        $pluginManager = parent::getPluginManager();
60
61 152
        foreach (self::$invokableClasses as $invokableClass) {
62
63 152
            $segments = explode('\\', $invokableClass);
64 152
            $name     = array_pop($segments);
65
66 152
            $pluginManager->setInvokableClass($name, $invokableClass);
67 152
        }
68
69 152
        return $pluginManager;
70
    }
71
72
    /**
73
     * Statically call a Validator and throw exception on failure
74
     *
75
     * @param mixed  $value     Value
76
     * @param string $className Class name
77
     * @param array  $options   Options
78
     *
79
     * @return bool
80
     *
81
     * @throws InvalidArgumentException
82
     */
83 153
    public static function execute($value, $className, array $options = [])
84
    {
85 153
        if (count($options) > 0 && array_values($options) === $options) {
86 1
            throw new InvalidArgumentException(
87
                'Invalid options provided via $options argument; must be an associative array'
88 1
            );
89
        }
90
91 152
        $plugins = static::getPluginManager();
92
93 152
        $validator = $plugins->get($className, $options);
94
95 151
        $ret = $validator->isValid($value);
96
97 151
        if (false === $ret) {
98 43
            $message  = null;
99 43
            $messages = $validator->getMessages();
100 43
            if (count($messages) > 0) {
101 43
                $message = array_shift($messages);
102 43
            }
103 43
            throw new InvalidArgumentException($message);
104
        }
105
106 134
        return $ret;
107
    }
108
109
}