Completed
Push — master ( ab2db9...9b8f8b )
by Mikaël
23:04
created

Rules::applyRules()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 11
nc 6
nop 2
dl 0
loc 14
ccs 13
cts 13
cp 1
crap 8
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\File\Validation;
4
5
use WsdlToPhp\PackageGenerator\Container\PhpElement\Method as MethodContainer;
6
use WsdlToPhp\PackageGenerator\Generator\Generator;
7
use WsdlToPhp\PhpGenerator\Element\PhpMethod;
8
use WsdlToPhp\PackageGenerator\File\AbstractModelFile;
9
use WsdlToPhp\PackageGenerator\Model\StructAttribute;
10
11
class Rules
12
{
13
14
    /**
15
     * @var StructAttribute
16
     */
17
    protected $attribute;
18
19
    /**
20
     * @var AbstractModelFile
21
     */
22
    protected $file;
23
24
    /**
25
     * @var PhpMethod
26
     */
27
    protected $method;
28
29
    /**
30
     * @var MethodContainer
31
     */
32
    protected $methods;
33
    /**
34
     * @param AbstractModelFile $file
35
     * @param PhpMethod $method
36
     * @param StructAttribute $attribute
37
     */
38 804
    public function __construct(AbstractModelFile $file, PhpMethod $method, StructAttribute $attribute, MethodContainer $methods)
39
    {
40 804
        $this->file = $file;
41 804
        $this->method = $method;
42 804
        $this->attribute = $attribute;
43 804
        $this->methods = $methods;
44 804
    }
45
46
    /**
47
     * @param string $parameterName
48
     * @param bool $itemType
49
     */
50 240
    public function applyRules($parameterName, $itemType = false)
51
    {
52 240
        if ($this->attribute->isArray() && !$itemType) {
53 114
            $this->getArrayRule()->applyRule($parameterName, null, $itemType);
54 240
        } elseif ($this->attribute->isList() && !$itemType) {
55 12
            $this->getListRule()->applyRule($parameterName, null, $itemType);
56 240
        } elseif ($this->getFile()->getRestrictionFromStructAttribute($this->attribute)) {
57 108
            $this->getEnumerationRule()->applyRule($parameterName, null);
58 237
        } elseif ($itemType) {
59 108
            $this->getItemTypeRule()->applyRule($parameterName, $itemType);
60 216
        } elseif (($rule = $this->getRule($this->getFile()->getStructAttributeTypeAsPhpType($this->attribute))) instanceof AbstractRule) {
61 156
            $rule->applyRule($parameterName, null, $itemType);
62 78
        }
63 240
        $this->applyRulesFromAttribute($parameterName, $itemType);
64 240
    }
65
66
    /**
67
     * Generic method to apply rules from current model
68
     * @param string $parameterName
69
     * @param bool $itemType
70
     */
71 240
    protected function applyRulesFromAttribute($parameterName, $itemType = false)
72
    {
73 240
        foreach ($this->attribute->getMeta() as $metaName => $metaValue) {
74 204
            $rule = $this->getRule($metaName);
75 204
            if ($rule instanceof AbstractRule) {
76 108
                $rule->applyRule($parameterName, $metaValue, $itemType);
77 30
            }
78 120
        }
79 240
    }
80
81
    /**
82
     * @param string $name
83
     * @return AbstractRule|null
84
     */
85 240
    protected function getRule($name)
86
    {
87 240
        if (is_string($name)) {
0 ignored issues
show
introduced by
The condition is_string($name) is always true.
Loading history...
88 240
            $className = sprintf('%s\%sRule', __NAMESPACE__, ucfirst($name));
89 240
            if (class_exists($className)) {
90 210
                return new $className($this);
91
            }
92 120
        }
93 240
        return null;
94
    }
95
96
    /**
97
     * @return ArrayRule
98
     */
99 114
    public function getArrayRule()
100
    {
101 114
        return $this->getRule('array');
102
    }
103
104
    /**
105
     * @return EnumerationRule
106
     */
107 114
    public function getEnumerationRule()
108
    {
109 114
        return $this->getRule('enumeration');
110
    }
111
112
    /**
113
     * @return ItemTypeRule
114
     */
115 108
    public function getItemTypeRule()
116
    {
117 108
        return $this->getRule('itemType');
118
    }
119
120
    /**
121
     * @return ListRule
122
     */
123 12
    public function getListRule()
124
    {
125 12
        return $this->getRule('list');
126
    }
127
    /**
128
     * @return StructAttribute
129
     */
130 234
    public function getAttribute()
131
    {
132 234
        return $this->attribute;
133
    }
134
135
    /**
136
     * @param StructAttribute $attribute
137
     * @return Rules
138
     */
139 6
    public function setAttribute(StructAttribute $attribute)
140
    {
141 6
        $this->attribute = $attribute;
142 6
        return $this;
143
    }
144
145
    /**
146
     * @return AbstractModelFile
147
     */
148 288
    public function getFile()
149
    {
150 288
        return $this->file;
151
    }
152
153
    /**
154
     * @return PhpMethod
155
     */
156 774
    public function getMethod()
157
    {
158 774
        return $this->method;
159
    }
160
161
    /**
162
     * @param PhpMethod $method
163
     * @return Rules
164
     */
165 114
    public function setMethod(PhpMethod $method)
166
    {
167 114
        $this->method = $method;
168 114
        return $this;
169
    }
170
    /**
171
     * @return MethodContainer
172
     */
173 150
    public function getMethods()
174
    {
175 150
        return $this->methods;
176
    }
177
    /**
178
     * @return Generator
179
     */
180 6
    public function getGenerator()
181
    {
182 6
        return $this->file->getGenerator();
183
    }
184
}
185