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

EavFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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\_ENTITY;
16
use Drobotik\Eav\Enum\_GROUP;
17
use Drobotik\Eav\Enum\_PIVOT;
18
use Drobotik\Eav\Enum\_SET;
19
use Drobotik\Eav\Enum\ATTR_TYPE;
20
use Drobotik\Eav\Model\AttributeGroupModel;
21
use Drobotik\Eav\Model\AttributeModel;
22
use Drobotik\Eav\Model\AttributeSetModel;
23
use Drobotik\Eav\Model\DomainModel;
24
use Drobotik\Eav\Model\EntityModel;
25
use Drobotik\Eav\Model\PivotModel;
26
use Drobotik\Eav\Model\ValueBase;
27
use Drobotik\Eav\Result\Result;
28
use Faker\Generator;
29
30
class EavFactory
31
{
32
    private Generator $faker;
33
34
    public function __construct()
35
    {
36
        $this->faker = \Faker\Factory::create();
37
    }
38
39 3
    public function createDomain(array $data = []): int
40
    {
41 3
        $defaultData = [
42 3
            _DOMAIN::NAME->column() => $this->faker->word(),
43 3
        ];
44 3
        $input = array_merge($defaultData, $data);
45 3
        $model = new DomainModel();
46 3
        return $model->create($input);
47
    }
48
49 2
    public function createEntity(?int $domainKey = null, ?int $setKey = null): int
50
    {
51 2
        if (is_null($domainKey))
52
        {
53 1
            $domainKey = $this->createDomain();
54
        }
55 2
        if (is_null($setKey))
56
        {
57 1
            $setKey = $this->createAttributeSet($domainKey);
58
        }
59 2
        $model = new EntityModel();
60 2
        return $model->create([
61 2
            _ENTITY::DOMAIN_ID->column() => $domainKey,
62 2
            _ENTITY::ATTR_SET_ID->column() => $setKey
63 2
        ]);
64
    }
65
66 3
    public function createAttributeSet(?int $domainKey = null, array $data = []): int
67
    {
68 3
        if (is_null($domainKey)) {
69 1
            $domainKey = $this->createDomain();
70
        }
71 3
        $defaultData = [
72 3
            _SET::NAME->column() => $this->faker->word(),
73 3
        ];
74 3
        $input = array_merge($defaultData, $data);
75 3
        $model = new AttributeSetModel();
76 3
        return $model->create([
77 3
            _SET::DOMAIN_ID->column() => $domainKey,
78 3
            _SET::NAME->column() => $input[_SET::NAME->column()]
79 3
        ]);
80
    }
81
82 3
    public function createGroup(?int $setKey = null, array $data = []): int
83
    {
84 3
        if (is_null($setKey)) {
85 1
            $setKey = $this->createAttributeSet();
86
        }
87 3
        $defaultData = [
88 3
            _GROUP::SET_ID->column() => $setKey,
89 3
            _GROUP::NAME->column() => $this->faker->word(),
90 3
        ];
91 3
        $input = array_merge($defaultData, $data);
92 3
        $model = new AttributeGroupModel();
93
94 3
        return $model->create([
95 3
            _GROUP::SET_ID->column() => $setKey,
96 3
            _GROUP::NAME->column() => $input[_SET::NAME->column()]
97 3
        ]);
98
    }
99
100 3
    public function createAttribute(?int $domainKey = null, array $data = []): int
101
    {
102 3
        if (is_null($domainKey)) {
103 1
            $domainKey = $this->createDomain();
104
        }
105 3
        $defaultData = [
106 3
            _ATTR::DOMAIN_ID->column() => $domainKey,
107 3
            _ATTR::NAME->column() => $this->faker->slug(2),
108 3
            _ATTR::TYPE->column() => _ATTR::TYPE->default(),
109 3
            _ATTR::STRATEGY->column() => _ATTR::STRATEGY->default(),
110 3
            _ATTR::SOURCE->column() => _ATTR::SOURCE->default(),
111 3
            _ATTR::DEFAULT_VALUE->column() => _ATTR::DEFAULT_VALUE->default(),
112 3
            _ATTR::DESCRIPTION->column() => _ATTR::DESCRIPTION->default(),
113 3
        ];
114 3
        $input = array_merge($defaultData, $data);
115 3
        $model = new AttributeModel();
116 3
        return $model->create([
117 3
            _ATTR::DOMAIN_ID->column() => $domainKey,
118 3
            _ATTR::NAME->column() => $input[_ATTR::NAME->column()],
119 3
            _ATTR::TYPE->column() => $input[_ATTR::TYPE->column()],
120 3
            _ATTR::STRATEGY->column() => $input[_ATTR::STRATEGY->column()],
121 3
            _ATTR::SOURCE->column() => $input[_ATTR::SOURCE->column()],
122 3
            _ATTR::DEFAULT_VALUE->column() => $input[_ATTR::DEFAULT_VALUE->column()],
123 3
            _ATTR::DESCRIPTION->column() => $input[_ATTR::DESCRIPTION->column()],
124 3
        ]);
125
    }
126
127 1
    public function createPivot(int $domainKey, int $setKey, int $groupKey, int $attributeKey): int
128
    {
129 1
        $model = new PivotModel();
130 1
        return $model->create([
131 1
            _PIVOT::DOMAIN_ID->column() => $domainKey,
132 1
            _PIVOT::SET_ID->column() => $setKey,
133 1
            _PIVOT::GROUP_ID->column() => $groupKey,
134 1
            _PIVOT::ATTR_ID->column() => $attributeKey
135 1
        ]);
136
    }
137
138 5
    public function createValue(ATTR_TYPE $type, int $domainKey, int $entityKey, int $attributeKey, $value): ValueBase
139
    {
140 5
        $model = $type->model();
141 5
        $model->setDomainKey($domainKey)
142 5
            ->setEntityKey($entityKey)
143 5
            ->setAttrKey($attributeKey)
144 5
            ->setValue($value);
145
        
146 5
        $model->save();
147 5
        $model->refresh();
148
149 5
        return $model;
150
    }
151
152 1
    public function createEavEntity(array $config, int $domainKey, int $setKey): Result
153
    {
154 1
        $result = new Result();
155 1
        $result->created();
156 1
        $factory = new EntityFactory();
157 1
        $result->setData($factory->create($config, $domainKey, $setKey));
158
159 1
        return $result;
160
    }
161
}
162