| Total Complexity | 10 |
| Total Lines | 48 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | final class Factory |
||
| 8 | { |
||
| 9 | private TypeMapper $ruleTypeMapper; |
||
| 10 | |||
| 11 | public function __construct(TypeMapper $typeMapper = null) |
||
| 12 | { |
||
| 13 | $this->ruleTypeMapper = $typeMapper ?? new TypeMapper(); |
||
| 14 | } |
||
| 15 | |||
| 16 | public function createFromArray(array $fields): RuleInterface |
||
| 17 | { |
||
| 18 | $ruleType = $this->normalizeRuleType($fields['type']); |
||
| 19 | |||
| 20 | /** @var \RemotelyLiving\Doorkeeper\Rules\RuleInterface $rule */ |
||
| 21 | $rule = isset($fields['value']) ? new $ruleType($fields['value']) : new $ruleType(); |
||
| 22 | |||
| 23 | return isset($fields['prerequisites']) ? $this->addPrerequisites($rule, $fields['prerequisites']) : $rule; |
||
| 24 | } |
||
| 25 | |||
| 26 | private function addPrerequisites(RuleInterface $rule, array $prequisites): RuleInterface |
||
| 27 | { |
||
| 28 | foreach ($prequisites as $prequisite) { |
||
| 29 | $preReqType = $this->normalizeRuleType($prequisite['type']); |
||
| 30 | $preReq = isset($prequisite['value']) ? new $preReqType($prequisite['value']) : new $preReqType(); |
||
| 31 | |||
| 32 | $rule->addPrerequisite($preReq); |
||
| 33 | } |
||
| 34 | |||
| 35 | return $rule; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param string|int $type |
||
| 40 | */ |
||
| 41 | private function normalizeRuleType($type): string |
||
| 55 | } |
||
| 56 | } |
||
| 57 |