ValueAction::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 21
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 31
ccs 23
cts 23
cp 1
crap 2
rs 9.584
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\Value;
12
13
use Kuperwood\Eav\Enum\_VALUE;
14
use Kuperwood\Eav\Enum\ATTR_TYPE;
15
use Kuperwood\Eav\Result\Result;
16
use Kuperwood\Eav\Traits\ContainerTrait;
17
use Kuperwood\Eav\Traits\SingletonsTrait;
18
19
class ValueAction
20
{
21
    use SingletonsTrait;
22
    use ContainerTrait;
23
24 2
    public function create(): Result
25
    {
26 2
        $result = new Result();
27
28 2
        $container = $this->getAttributeContainer();
29 2
        $attribute = $container->getAttribute();
30 2
        $entity = $container->getAttributeSet()->getEntity();
31 2
        $valueManager = $container->getValueManager();
32 2
        $valueModel = $this->makeValueModel();
33 2
        $valueParser = $this->makeValueParser();
34 2
35 1
        if (!$valueManager->isRuntime()) {
36
            return $result->empty();
37
        }
38 1
39 1
        $type = $attribute->getType();
40
        $value = $valueManager->getRuntime();
41 1
42 1
        $valueKey = $valueModel->create(
43 1
            $type,
44 1
            $entity->getDomainKey(),
45 1
            $entity->getKey(),
46 1
            $attribute->getKey(),
47 1
            $value
48
        );
49 1
50 1
        $valueManager->setStored($valueParser->parse($type, $value))
51 1
            ->setKey($valueKey)
52
            ->clearRuntime();
53 1
54
        return $result->created();
55
    }
56 3
57
    public function find(): Result
58 3
    {
59 3
        $result = new Result();
60 3
        $container = $this->getAttributeContainer();
61 3
        $attribute = $container->getAttribute();
62 3
        $type = $attribute->getType();
63 3
        $attributeKey = $attribute->getKey();
64
        $entity = $container->getAttributeSet()->getEntity();
65 3
66 3
        $valueManager = $container->getValueManager();
67
        $valueModel = $this->makeValueModel();
68 3
69 1
        if (is_null($attributeKey) || !$entity->hasKey()) {
70
            return $result->empty();
71
        }
72 2
73 2
        $entityKey = $entity->getKey();
74
        $domainKey = $entity->getDomainKey();
75 2
76 2
        $record = $valueModel->find(
77 2
            $type,
78 2
            $domainKey,
79 2
            $entityKey,
80 2
            $attributeKey
81
        );
82 2
83 1
        if ($record === false) {
84
            return $result->notFound();
85
        }
86 1
87 1
        $valueManager
88 1
            ->setKey($record[_VALUE::ID])
89
            ->setStored($record[_VALUE::VALUE]);
90 1
91
        return $result->found();
92
    }
93 2
94
    public function update(): Result
95 2
    {
96 2
        $result = new Result();
97 2
        $container = $this->getAttributeContainer();
98 2
        $entity = $container->getAttributeSet()->getEntity();
99 2
        $attribute = $container->getAttribute();
100 2
        $type = $attribute->getType();
101 2
        $valueManager = $container->getValueManager();
102 2
        $valueModel = $this->makeValueModel();
103
104 2
        if (!$valueManager->isRuntime()) {
105 1
            return $result->empty();
106
        }
107
108 1
        $domainKey = $entity->getDomainKey();
109 1
        $entityKey = $entity->getKey();
110 1
        $attributeKey = $attribute->getKey();
111
112 1
        $value = $valueManager->getRuntime();
113
114 1
        $valueModel->update(
115 1
            $type,
116 1
            $domainKey,
117 1
            $entityKey,
118 1
            $attributeKey,
119 1
            $value
120 1
        );
121
122 1
        $valueManager->setStored($value)
123 1
            ->clearRuntime();
124
125 1
        return $result->updated();
126
    }
127
128 3
    public function delete(): Result
129
    {
130 3
        $result = new Result();
131 3
        $container = $this->getAttributeContainer();
132 3
        $entity = $container->getAttributeSet()->getEntity();
133 3
        $attribute = $container->getAttribute();
134 3
        $type = $attribute->getType();
135 3
        $valueManager = $container->getValueManager();
136 3
        $valueModel = $this->makeValueModel();
137
138 3
        if (!$valueManager->hasKey()) {
139 1
            return $result->empty();
140
        }
141
142 2
        $domainKey = $entity->getDomainKey();
143 2
        $entityKey = $entity->getKey();
144 2
        $attributeKey = $attribute->getKey();
145
146 2
        $deleted = $valueModel->destroy($type, $domainKey, $entityKey, $attributeKey);
147
148 2
        if ($deleted === 0) {
149 1
            return $result->notDeleted();
150
        }
151
152 1
        $valueManager->clearStored()
153 1
            ->clearRuntime()
154 1
            ->setKey(0);
155
156 1
        return $result->deleted();
157
    }
158
}
159