Passed
Push — master ( 4a4c08...26e14e )
by Aleksandr
03:54 queued 01:34
created

ValueAction::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3

Importance

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