Passed
Push — 2.x ( bef6c0...34f135 )
by Mikaël
03:54
created

AbstractRule::applyRule()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 2
nop 3
dl 0
loc 13
ccs 4
cts 4
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PackageGenerator\Generator\Generator;
6
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
7
use WsdlToPhp\PackageGenerator\Model\StructAttribute as StructAttributeModel;
8
use WsdlToPhp\PackageGenerator\File\AbstractModelFile;
9
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer;
10
11
abstract class AbstractRule
12
{
13
    /**
14
     * @var Rules
15
     */
16
    protected $rules;
17
    /**
18 750
     * @param Rules $rules
19
     */
20 750
    public function __construct(Rules $rules)
21 750
    {
22
        $this->rules = $rules;
23
    }
24
    /**
25
     * This method has to add the validation rule to the method's body
26
     * @param string $parameterName
27
     * @param mixed $value
28
     * @param bool $itemType
29
     * @return AbstractRule
30
     */
31
    final public function applyRule($parameterName, $value, $itemType = false)
32
    {
33 750
        $test = $this->testConditions($parameterName, $value, $itemType);
34
        if (!empty($test)) {
35 750
            $message = $this->exceptionMessageOnTestFailure($parameterName, $value, $itemType);
36
            $this->getMethod()
37
                ->addChild($this->getMethod()->getIndentedString(sprintf('// validation for constraint: %s', $this->name()), $itemType ? 1 : 0))
38
                ->addChild($this->getMethod()->getIndentedString(sprintf('if (%s) {', $test), $itemType ? 1 : 0))
39
                ->addChild($this->getMethod()->getIndentedString(sprintf('throw new \InvalidArgumentException(%s, __LINE__);', $message), $itemType ? 2 : 1))
40
                ->addChild($this->getMethod()->getIndentedString('}', $itemType ? 1 : 0));
41 750
            unset($message);
42
        }
43 750
        unset($test);
44 750
    }
45
    /**
46
     * Name of the validation rule
47
     * @return string
48
     */
49 750
    abstract public function name();
50
    /**
51 750
     * Inline tests of the validation rule
52
     * @param string $parameterName
53
     * @param mixed $value
54
     * @param bool $itemType
55
     * @return string
56 198
     */
57
    abstract public function testConditions($parameterName, $value, $itemType = false);
58 198
    /**
59
     * Message when test fails in order to throw the exception
60
     * @param string $parameterName
61
     * @param mixed $value
62
     * @param bool $itemType
63 198
     * @return string
64
     */
65 198
    abstract public function exceptionMessageOnTestFailure($parameterName, $value, $itemType = false);
66
    /**
67
     * @return Rules
68
     */
69
    public function getRules()
70
    {
71
        return $this->rules;
72
    }
73
    /**
74
     * @return PhpMethod
75
     */
76
    public function getMethod()
77
    {
78
        return $this->rules->getMethod();
79
    }
80
    /**
81
     * @return MethodContainer
82
     */
83
    public function getMethods()
84
    {
85
        return $this->rules->getMethods();
86
    }
87
    /**
88
     * @return AbstractModelFile
89
     */
90
    public function getFile()
91
    {
92
        return $this->rules->getFile();
93
    }
94
    /**
95
     * @return StructAttributeModel
96
     */
97
    public function getAttribute()
98
    {
99
        return $this->rules->getAttribute();
100
    }
101
    /**
102
     * @return Generator
103
     */
104
    public function getGenerator()
105
    {
106
        return $this->rules->getGenerator();
107
    }
108
}
109