Passed
Push — master ( 17e6cc...350d8b )
by Aleksandr
41:29 queued 06:24
created

ValueAction   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 89
dl 0
loc 138
ccs 93
cts 93
cp 1
rs 10
c 1
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 33 2
A create() 0 30 2
A delete() 0 29 3
A find() 0 35 4
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
        $parser = $this->makeValueParser();
34 2
        if (!$valueManager->isRuntime()) {
35 1
            return $result->empty();
36
        }
37
38 1
        $type = $attribute->getType();
39 1
        $value = $parser->parse($type, $valueManager->getRuntime());
40
41 1
        $valueKey = $valueModel->create(
42 1
            $type,
43 1
            $entity->getDomainKey(),
44 1
            $entity->getKey(),
45 1
            $attribute->getKey(),
46 1
            $value
47 1
        );
48
49 1
        $valueManager->setStored($value)
50 1
            ->setKey($valueKey)
51 1
            ->clearRuntime();
52
53 1
        return $result->created();
54
    }
55
56 3
    public function find(): Result
57
    {
58 3
        $result = new Result();
59 3
        $container = $this->getAttributeContainer();
60 3
        $attribute = $container->getAttribute();
61 3
        $type = $attribute->getType();
62 3
        $attributeKey = $attribute->getKey();
63 3
        $entity = $container->getAttributeSet()->getEntity();
64
65 3
        $valueManager = $container->getValueManager();
66 3
        $valueModel = $this->makeValueModel();
67
68 3
        if (is_null($attributeKey) || !$entity->hasKey()) {
69 1
            return $result->empty();
70
        }
71
72 2
        $entityKey = $entity->getKey();
73 2
        $domainKey = $entity->getDomainKey();
74
75 2
        $record = $valueModel->find(
76 2
            $type,
77 2
            $domainKey,
78 2
            $entityKey,
79 2
            $attributeKey
80 2
        );
81
82 2
        if ($record === false) {
83 1
            return $result->notFound();
84
        }
85
86 1
        $valueManager
87 1
            ->setKey($record[_VALUE::ID])
88 1
            ->setStored($record[_VALUE::VALUE]);
89
90 1
        return $result->found();
91
    }
92
93 2
    public function update(): Result
94
    {
95 2
        $result = new Result();
96 2
        $container = $this->getAttributeContainer();
97 2
        $entity = $container->getAttributeSet()->getEntity();
98 2
        $attribute = $container->getAttribute();
99 2
        $type = $attribute->getType();
100 2
        $valueManager = $container->getValueManager();
101 2
        $valueModel = $this->makeValueModel();
102 2
        $valueParser = $this->makeValueParser();
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 = $valueParser->parse($type, $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