Completed
Push — feature/issue-165 ( 83f642...d66719 )
by Mikaël
23:02 queued 18s
created

Rules::applyRulesFromModel()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6

Importance

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