Completed
Push — master ( 1bff5d...2a2446 )
by Mikaël
75:38 queued 45:53
created

Rules::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PackageGenerator\Model\StructAttribute;
6
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
7
use WsdlToPhp\PackageGenerator\File\AbstractModelFile;
8
9
class Rules
10
{
11
    /**
12
     * @var StructAttribute
13
     */
14
    private $attribute;
15
    /**
16
     * @var AbstractModelFile
17
     */
18
    private $file;
19
    /**
20
     * @var PhpMethod
21
     */
22
    private $method;
23
    /**
24
     * @param AbstractModelFile $file
25
     * @param PhpMethod $method
26
     * @param StructAttribute $attribute
27
     */
28 100
    public function __construct(AbstractModelFile $file, PhpMethod $method, StructAttribute $attribute)
29
    {
30 75
        $this
31 100
            ->setFile($file)
32 100
            ->setMethod($method)
33 100
            ->setAttribute($attribute);
34 100
    }
35
    /**
36
     * @param string $parameterName
37
     * @param bool $itemType
38
     */
39 100
    public function applyRules($parameterName, $itemType = false)
40
    {
41 100
        foreach ($this->getAttribute()->getMeta() as $metaName=>$metaValue) {
42 100
            $rule = $this->getRule($metaName);
43 100
            if ($rule instanceof AbstractRule) {
44 37
                $rule->applyRule($parameterName, $metaValue, $itemType);
45 12
            }
46 75
        }
47 100
        if ($this->getAttribute()->isArray() && !$itemType) {
48 60
            $this->getArrayRule()->applyRule($parameterName, null, $itemType);
49 100
        } elseif ($this->getFile()->getRestrictionFromStructAttribute($this->getAttribute())) {
50 48
            $this->getEnumerationRule()->applyRule($parameterName, null, $itemType);
51 99
        } elseif ($itemType) {
52 56
            $this->getItemTypeRule()->applyRule($parameterName, null, $itemType);
53 90
        } elseif (($rule = $this->getRule($this->getFile()->getStructAttributeTypeAsPhpType($this->getAttribute()))) instanceof AbstractRule) {
54 72
            $rule->applyRule($parameterName, null, $itemType);
55 54
        }
56 100
    }
57
    /**
58
     * @param string $metaName
59
     * @return AbstractRule
60
     */
61 100
    protected function getRule($metaName)
62
    {
63 100
        if (is_string($metaName)) {
64 100
            $className = sprintf('%s\%sRule', __NAMESPACE__, ucfirst($metaName));
65 100
            if (class_exists($className)) {
66 100
                return new $className($this);
67
            }
68 75
        }
69 100
        return null;
70
    }
71
    /**
72
     * @return ArrayRule
73
     */
74 60
    public function getArrayRule()
75
    {
76 60
        return $this->getRule('array');
77
    }
78
    /**
79
     * @return EnumerationRule
80
     */
81 48
    public function getEnumerationRule()
82
    {
83 48
        return $this->getRule('enumeration');
84
    }
85
    /**
86
     * @return ItemTypeRule
87
     */
88 56
    public function getItemTypeRule()
89
    {
90 56
        return $this->getRule('itemType');
91
    }
92
    /**
93
     * @return StructAttribute
94
     */
95 100
    public function getAttribute()
96
    {
97 100
        return $this->attribute;
98
    }
99
    /**
100
     * @param StructAttribute $attribute
101
     * @return Rules
102
     */
103 100
    public function setAttribute(StructAttribute $attribute)
104
    {
105 100
        $this->attribute = $attribute;
106 100
        return $this;
107
    }
108
    /**
109
     * @return AbstractModelFile
110
     */
111 100
    public function getFile()
112
    {
113 100
        return $this->file;
114
    }
115
    /**
116
     * @param AbstractModelFile $file
117
     * @return Rules
118
     */
119 100
    public function setFile(AbstractModelFile $file)
120
    {
121 100
        $this->file = $file;
122 100
        return $this;
123
    }
124
    /**
125
     * @return PhpMethod
126
     */
127 100
    public function getMethod()
128
    {
129 100
        return $this->method;
130
    }
131
    /**
132
     * @param PhpMethod $method
133
     * @return Rules
134
     */
135 100
    public function setMethod(PhpMethod $method)
136
    {
137 100
        $this->method = $method;
138 100
        return $this;
139
    }
140
}
141