Passed
Push — master ( c062f3...83edb5 )
by Aleksandr
02:32
created

EntityFactory   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 79.63%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 57
c 1
b 0
f 1
dl 0
loc 85
ccs 43
cts 54
cp 0.7963
rs 10
wmc 13

1 Method

Rating   Name   Duplication   Size   Complexity  
F create() 0 80 13
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\Factory;
12
13
use Drobotik\Eav\Enum\_ATTR;
14
use Drobotik\Eav\Enum\_PIVOT;
15
use Drobotik\Eav\Enum\ATTR_FACTORY;
16
use Drobotik\Eav\Enum\ATTR_TYPE;
17
use Drobotik\Eav\Exception\AttributeException;
18
use Drobotik\Eav\Exception\EntityFactoryException;
19
use Drobotik\Eav\Result\EntityFactoryResult;
20
use Drobotik\Eav\Trait\RepositoryTrait;
21
use Drobotik\Eav\Trait\SingletonsTrait;
22
23
class EntityFactory
24
{
25
    use RepositoryTrait;
26
    use SingletonsTrait;
27
28 11
    public function create(array $fields, int $domainKey, int $setKey): EntityFactoryResult
29
    {
30 11
        $result = new EntityFactoryResult();
31 11
        $valueRepo = $this->makeValueRepository();
32 11
        $attributeModel = $this->makeAttributeModel();
33 11
        $groupModel = $this->makeGroupModel();
34 11
        $pivotModel = $this->makePivotModel();
35 11
        $factory = $this->makeEavFactory();
36 11
        foreach($fields as $field) {
37 10
            if (!key_exists(ATTR_FACTORY::GROUP->field(), $field)) {
38 1
                throw new EntityFactoryException("Group key must be provided!");
39
            }
40
        }
41 10
        $groups = array_unique(array_column($fields, ATTR_FACTORY::GROUP->field()));
42 10
        foreach($groups as $groupKey)
43
        {
44 9
            if(!$groupModel->checkGroupInAttributeSet($setKey, $groupKey))
45
            {
46 1
                throw new EntityFactoryException("This group is not belongs to attribute set");
47
            }
48
        }
49
50 9
        $entityKey = $this->makeEavFactory()->createEntity($domainKey, $setKey);
51 9
        $result->setEntityKey($entityKey);
52
53 9
        foreach ($fields as $field) {
54
55 8
            if (!key_exists(ATTR_FACTORY::ATTRIBUTE->field(), $field)) {
56 1
                EntityFactoryException::undefinedAttributeArray();
57
            }
58 7
            $attrConfig = $field[ATTR_FACTORY::ATTRIBUTE->field()];
59 7
            if (!key_exists(_ATTR::NAME->column(), $attrConfig)) {
60 1
                AttributeException::undefinedAttributeName();
61
            }
62 6
            if (!key_exists(_ATTR::TYPE->column(), $attrConfig)) {
63 1
                AttributeException::undefinedAttributeType();
64
            }
65
66 5
            $attrName = $attrConfig[_ATTR::NAME->column()];
67 5
            $attrType = ATTR_TYPE::getCase($attrConfig[_ATTR::TYPE->column()]);
68 4
            $attrRecord = $attributeModel->findByName($attrConfig[_ATTR::NAME->column()], $domainKey);
69
70 4
            if ($attrRecord !== false) {
71
                $attrKey = $attrRecord[_ATTR::ID->column()];
72
                $attrData = array_intersect_key([
73
                    _ATTR::NAME->column() => null,
74
                    _ATTR::TYPE->column() => null,
75
                    _ATTR::STRATEGY->column() => null,
76
                    _ATTR::SOURCE->column() => null,
77
                    _ATTR::DEFAULT_VALUE->column() => null,
78
                    _ATTR::DESCRIPTION->column() => null
79
                ], $attrConfig);
80
                $attributeModel->update($attrKey, $attrData);
81
            } else {
82 4
                $attrKey = $factory->createAttribute($domainKey, $attrConfig);
83
            }
84
85 4
            $result->addAttribute([
86 4
                _ATTR::ID->column() => $attrKey,
87 4
                _ATTR::NAME->column() => $attrConfig[_ATTR::NAME->column()]
88 4
            ]);
89
90 4
            $pivotRecord = $pivotModel->findOne($domainKey, $setKey, $field[ATTR_FACTORY::GROUP->field()], $attrKey);
91 4
            if($pivotRecord === false)
92 4
                $pivotKey = $factory->createPivot($domainKey, $setKey, $field[ATTR_FACTORY::GROUP->field()], $attrKey);
93
            else
94
                $pivotKey = $pivotRecord[_PIVOT::ID->column()];
95
96 4
            $result->addPivot($attrName, $pivotKey);
97
98 4
            $valueRecord = isset($field[ATTR_FACTORY::VALUE->field()])
99 1
                ? $valueRepo->updateOrCreate($domainKey, $entityKey, $attrKey, $attrType, $field[ATTR_FACTORY::VALUE->field()])
100 3
                : null;
101
102 4
            if(!is_null($valueRecord)) {
103 1
                $result->addValue($attrName, $valueRecord);
104
            }
105
        }
106
107 5
        return $result;
108
    }
109
}
110