Passed
Push — feature/issue-165 ( 2b7e50...101bca )
by Mikaël
14:10
created

AbstractRule::applyRule()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6

Importance

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