Passed
Push — master ( da1b99...92df87 )
by Aleksandr
03:12
created

EavFactory   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 91
c 1
b 0
f 1
dl 0
loc 151
ccs 102
cts 102
cp 1
rs 10
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A createValue() 0 12 1
A createAttributeSet() 0 18 2
A createPivot() 0 12 1
A createDomain() 0 11 1
A createGroup() 0 19 2
A __construct() 0 3 1
A createEavEntity() 0 8 1
A createAttribute() 0 29 2
A createEntity() 0 19 3
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\_DOMAIN;
15
use Drobotik\Eav\Enum\_GROUP;
16
use Drobotik\Eav\Enum\_SET;
17
use Drobotik\Eav\Enum\ATTR_TYPE;
18
use Drobotik\Eav\Model\AttributeGroupModel;
19
use Drobotik\Eav\Model\AttributeModel;
20
use Drobotik\Eav\Model\AttributeSetModel;
21
use Drobotik\Eav\Model\DomainModel;
22
use Drobotik\Eav\Model\EntityModel;
23
use Drobotik\Eav\Model\PivotModel;
24
use Drobotik\Eav\Model\ValueBase;
25
use Drobotik\Eav\Result\Result;
26
use Faker\Generator;
27
28
class EavFactory
29
{
30
    private Generator $faker;
31
32 3
    public function __construct()
33
    {
34 3
        $this->faker = \Faker\Factory::create();
35
    }
36
37 3
    public function createDomain(array $data = []): DomainModel
38
    {
39 3
        $defaultData = [
40 3
            _DOMAIN::NAME->column() => $this->faker->word(),
41 3
        ];
42 3
        $input = array_merge($defaultData, $data);
43 3
        $model = new DomainModel();
44 3
        $model->setName($input[_DOMAIN::NAME->column()]);
45 3
        $model->create();
46
47 3
        return $model;
48
    }
49
50 2
    public function createEntity(?int $domainKey = null, ?int $setKey = null): EntityModel
51
    {
52 2
        if (is_null($domainKey))
53
        {
54 1
            $domain = $this->createDomain();
55 1
            $domainKey = $domain->getKey();
56
        }
57 2
        if (is_null($setKey))
58
        {
59 1
            $attrSet = $this->createAttributeSet($domainKey);
60 1
            $setKey = $attrSet->getKey();
61
        }
62 2
        $model = new EntityModel();
63 2
        $model->setDomainKey($domainKey);
64 2
        $model->setAttrSetKey($setKey);
65 2
        $model->save();
66 2
        $model->refresh();
67
68 2
        return $model;
69
    }
70
71 3
    public function createAttributeSet(?int $domainKey = null, array $data = []): AttributeSetModel
72
    {
73 3
        if (is_null($domainKey)) {
74 2
            $domain = $this->createDomain();
75 2
            $domainKey = $domain->getKey();
76
        }
77 3
        $defaultData = [
78 3
            _SET::NAME->column() => $this->faker->word(),
79 3
        ];
80 3
        $input = array_merge($defaultData, $data);
81 3
        $model = new AttributeSetModel();
82 3
        $model->setDomainKey($domainKey)
83 3
            ->setName($input[_SET::NAME->column()]);
84
        
85 3
        $model->save();
86 3
        $model->refresh();
87
88 3
        return $model;
89
    }
90
91 3
    public function createGroup(?int $setKey = null, array $data = []): AttributeGroupModel
92
    {
93 3
        if (is_null($setKey)) {
94 2
            $set = $this->createAttributeSet();
95 2
            $setKey = $set->getKey();
96
        }
97 3
        $defaultData = [
98 3
            _GROUP::SET_ID->column() => $setKey,
99 3
            _GROUP::NAME->column() => $this->faker->word(),
100 3
        ];
101 3
        $input = array_merge($defaultData, $data);
102 3
        $model = new AttributeGroupModel();
103 3
        $model->setAttrSetKey($setKey)
104 3
            ->setName($input[_SET::NAME->column()]);
105
        
106 3
        $model->save();
107 3
        $model->refresh();
108
109 3
        return $model;
110
    }
111
112 3
    public function createAttribute(?int $domainKey = null, array $data = []): AttributeModel
113
    {
114 3
        if (is_null($domainKey)) {
115 2
            $domain = $this->createDomain();
116 2
            $domainKey = $domain->getKey();
117
        }
118 3
        $defaultData = [
119 3
            _ATTR::DOMAIN_ID->column() => $domainKey,
120 3
            _ATTR::NAME->column() => $this->faker->slug(2),
121 3
            _ATTR::TYPE->column() => _ATTR::TYPE->default(),
122 3
            _ATTR::STRATEGY->column() => _ATTR::STRATEGY->default(),
123 3
            _ATTR::SOURCE->column() => _ATTR::SOURCE->default(),
124 3
            _ATTR::DEFAULT_VALUE->column() => _ATTR::DEFAULT_VALUE->default(),
125 3
            _ATTR::DESCRIPTION->column() => _ATTR::DESCRIPTION->default(),
126 3
        ];
127 3
        $input = array_merge($defaultData, $data);
128 3
        $model = new AttributeModel();
129 3
        $model->setName($input[_ATTR::NAME->column()])
130 3
            ->setDomainKey($domainKey)
131 3
            ->setType($input[_ATTR::TYPE->column()])
132 3
            ->setStrategy($input[_ATTR::STRATEGY->column()])
133 3
            ->setSource($input[_ATTR::SOURCE->column()])
134 3
            ->setDefaultValue($input[_ATTR::DEFAULT_VALUE->column()])
135 3
            ->setDescription($input[_ATTR::DESCRIPTION->column()]);
136
        
137 3
        $model->save();
138 3
        $model->refresh();
139
140 3
        return $model;
141
    }
142
143 1
    public function createPivot(int $domainKey, int $setKey, int $groupKey, int $attributeKey): PivotModel
144
    {
145 1
        $model = new PivotModel();
146 1
        $model->setDomainKey($domainKey)
147 1
            ->setAttrSetKey($setKey)
148 1
            ->setGroupKey($groupKey)
149 1
            ->setAttrKey($attributeKey);
150
        
151 1
        $model->save();
152 1
        $model->refresh();
153
154 1
        return $model;
155
    }
156
157 5
    public function createValue(ATTR_TYPE $type, int $domainKey, int $entityKey, int $attributeKey, $value): ValueBase
158
    {
159 5
        $model = $type->model();
160 5
        $model->setDomainKey($domainKey)
161 5
            ->setEntityKey($entityKey)
162 5
            ->setAttrKey($attributeKey)
163 5
            ->setValue($value);
164
        
165 5
        $model->save();
166 5
        $model->refresh();
167
168 5
        return $model;
169
    }
170
171 1
    public function createEavEntity(array $config, int $domainKey, int $setKey): Result
172
    {
173 1
        $result = new Result();
174 1
        $result->created();
175 1
        $factory = new EntityFactory();
176 1
        $result->setData($factory->create($config, $domainKey, $setKey));
177
178 1
        return $result;
179
    }
180
}
181