Strategy::delete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the eav package.
4
 *
5
 * @author    Alex Kuperwood <[email protected]>
6
 * @copyright 2025 Alex Kuperwood
7
 * @license   https://opensource.org/license/mit  The MIT License
8
 */
9
declare(strict_types=1);
10
11
namespace Kuperwood\Eav;
12
13
use Kuperwood\Eav\Enum\_RESULT;
14
use Kuperwood\Eav\Interfaces\EavStrategyInterface;
15
use Kuperwood\Eav\Interfaces\StrategyInterface;
16
use Kuperwood\Eav\Result\Result;
17
use Kuperwood\Eav\Traits\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->validateAll($valueValidator->getValidatedData(), $valueValidator->getRules());
87
88 2
        if (count($violations) > 0) {
89 1
            $violationData = [];
90 1
            foreach($violations as $key => $violation) {
91 1
                $violationData[$key] = $violation->getMessage();
92
            }
93 1
            return $result->validationFails()
94 1
                ->setData($violationData);
95
        }
96
97 1
        return $result->setCode(_RESULT::VALIDATION_PASSED)
98 1
            ->setMessage(_RESULT::message(_RESULT::VALIDATION_PASSED));
99
    }
100
101 2
    public function save(): Result
102
    {
103 2
        $entity = $this->getAttributeContainer()->getAttributeSet()->getEntity();
104
105 2
        return $entity->getKey()
106 1
            ? $this->update()
107 2
            : $this->create();
108
    }
109
110 1
    public function afterCreate(): void
111
    {
112 1
    }
113
114 1
    public function beforeCreate(): void
115
    {
116 1
    }
117
118 1
    public function beforeUpdate(): void
119
    {
120 1
    }
121
122 1
    public function afterUpdate(): void
123
    {
124 1
    }
125
126 1
    public function beforeDelete(): void
127
    {
128 1
    }
129
130 1
    public function afterDelete(): void
131
    {
132 1
    }
133
134 1
    public function rules(): ?array
135
    {
136 1
        return null;
137
    }
138
139 1
    public function isCreate(): bool
140
    {
141 1
        return $this->create;
142
    }
143
144 1
    public function isUpdate(): bool
145
    {
146 1
        return $this->update;
147
    }
148
}
149