Passed
Push — master ( 3f1e0c...bf7a69 )
by Aleksandr
02:11
created

Strategy   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 51
dl 0
loc 127
ccs 68
cts 68
cp 1
rs 10
c 2
b 1
f 1
wmc 21

15 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 19 3
A delete() 0 10 1
A create() 0 16 2
A find() 0 3 1
A isCreate() 0 3 1
A save() 0 7 2
A validate() 0 17 3
A isUpdate() 0 3 1
A rules() 0 3 1
A beforeUpdate() 0 2 1
A beforeCreate() 0 2 1
A afterCreate() 0 2 1
A afterDelete() 0 2 1
A afterUpdate() 0 2 1
A beforeDelete() 0 2 1
1
<?php
2
/**
3
 * This file is part of the eav package.
4
 *
5
 * @author    Aleksandr Drobotik <[email protected]>
6
 * @copyright 2023 Aleksandr Drobotik
7
 * @license   https://opensource.org/license/mit  The MIT License
8
 */
9
declare(strict_types=1);
10
11
namespace Drobotik\Eav;
12
13
use Drobotik\Eav\Enum\_RESULT;
14
use Drobotik\Eav\Interface\EavStrategyInterface;
15
use Drobotik\Eav\Interface\StrategyInterface;
16
use Drobotik\Eav\Result\Result;
17
use Drobotik\Eav\Trait\ContainerTrait;
18
19
class Strategy implements StrategyInterface, EavStrategyInterface
20
{
21
    use ContainerTrait;
22
    public bool $create = true;
23
    public bool $update = true;
24
25 3
    public function create(): Result
26
    {
27 3
        $container = $this->getAttributeContainer();
28 3
        $valueAction = $container->getValueAction();
29
30 3
        if (!$this->isCreate()) {
31 1
            $container->getValueManager()->clearRuntime();
32
33 1
            return (new Result())->notAllowed();
34
        }
35
36 2
        $this->beforeCreate();
37 2
        $result = $valueAction->create();
38 2
        $this->afterCreate();
39
40 2
        return $result;
41
    }
42
43 5
    public function update(): Result
44
    {
45 5
        $container = $this->getAttributeContainer();
46 5
        $valueManager = $container->getValueManager();
47 5
        $valueAction = $container->getValueAction();
48
49 5
        if (!$this->isUpdate()) {
50 1
            $valueManager->clearRuntime();
51
52 1
            return (new Result())->notAllowed();
53
        }
54
55 4
        $this->beforeUpdate();
56 4
        $result = $valueManager->hasKey()
57 2
            ? $valueAction->update()
58 2
            : $valueAction->create();
59 4
        $this->afterUpdate();
60
61 4
        return $result;
62
    }
63
64 3
    public function delete(): Result
65
    {
66 3
        $container = $this->getAttributeContainer();
67 3
        $valueAction = $container->getValueAction();
68
69 3
        $this->beforeDelete();
70 3
        $result = $valueAction->delete();
71 3
        $this->afterDelete();
72
73 3
        return $result;
74
    }
75
76 1
    public function find(): Result
77
    {
78 1
        return $this->getAttributeContainer()->getValueAction()->find();
79
    }
80
81 2
    public function validate(): Result
82
    {
83 2
        $result = new Result();
84 2
        $valueValidator = $this->getAttributeContainer()->getValueValidator();
85 2
        $validator = $valueValidator->getValidator();
86 2
        $violations = $validator->validate($valueValidator->getValidatedData(), $valueValidator->getRules());
87 2
        if (count($violations) > 0) {
88 1
            $violationData = [];
89 1
            foreach($violations as $violation) {
90 1
                $violationData[$violation->getPropertyPath()] = $violation->getMessage();
91
            }
92 1
            return $result->validationFails()
93 1
                ->setData($violationData);
94
        }
95
96 1
        return $result->setCode(_RESULT::VALIDATION_PASSED->code())
97 1
            ->setMessage(_RESULT::VALIDATION_PASSED->message());
98
    }
99
100 2
    public function save(): Result
101
    {
102 2
        $entity = $this->getAttributeContainer()->getAttributeSet()->getEntity();
103
104 2
        return $entity->getKey()
105 1
            ? $this->update()
106 2
            : $this->create();
107
    }
108
109 1
    public function afterCreate(): void
110
    {
111 1
    }
112
113 1
    public function beforeCreate(): void
114
    {
115 1
    }
116
117 1
    public function beforeUpdate(): void
118
    {
119 1
    }
120
121 1
    public function afterUpdate(): void
122
    {
123 1
    }
124
125 1
    public function beforeDelete(): void
126
    {
127 1
    }
128
129 1
    public function afterDelete(): void
130
    {
131 1
    }
132
133 1
    public function rules(): ?array
134
    {
135 1
        return null;
136
    }
137
138 1
    public function isCreate(): bool
139
    {
140 1
        return $this->create;
141
    }
142
143 1
    public function isUpdate(): bool
144
    {
145 1
        return $this->update;
146
    }
147
}
148