|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the eav package. |
|
4
|
|
|
* @author Aleksandr Drobotik <[email protected]> |
|
5
|
|
|
* @copyright 2023 Aleksandr Drobotik |
|
6
|
|
|
* @license https://opensource.org/license/mit The MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Drobotik\Eav\Import\Content; |
|
11
|
|
|
|
|
12
|
|
|
use Drobotik\Eav\Enum\_ATTR; |
|
13
|
|
|
use Drobotik\Eav\Enum\_ENTITY; |
|
14
|
|
|
use Drobotik\Eav\Enum\ATTR_TYPE; |
|
15
|
|
|
use Drobotik\Eav\Exception\EntityException; |
|
16
|
|
|
use Drobotik\Eav\Trait\ImportContainerTrait; |
|
17
|
|
|
use Drobotik\Eav\Trait\SingletonsTrait; |
|
18
|
|
|
|
|
19
|
|
|
class Worker |
|
20
|
|
|
{ |
|
21
|
|
|
use ImportContainerTrait; |
|
22
|
|
|
use SingletonsTrait; |
|
23
|
|
|
|
|
24
|
|
|
private AttributeSet $attributeSet; |
|
25
|
|
|
private ValueSet $valueSet; |
|
26
|
|
|
private int $lineIndex; |
|
27
|
|
|
|
|
28
|
3 |
|
public function __construct() { |
|
29
|
3 |
|
$this->valueSet = new ValueSet(); |
|
30
|
3 |
|
$this->attributeSet = new AttributeSet(); |
|
31
|
3 |
|
$this->attributeSet->setWorker($this); |
|
32
|
3 |
|
$this->resetLineIndex(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
public function getValueSet(): ValueSet |
|
36
|
|
|
{ |
|
37
|
1 |
|
return $this->valueSet; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function getAttributeSet() : AttributeSet |
|
41
|
|
|
{ |
|
42
|
1 |
|
return $this->attributeSet; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function makeBulkValuesSet() : ValueSet |
|
46
|
|
|
{ |
|
47
|
1 |
|
return new ValueSet(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function incrementLineIndex(): void |
|
51
|
|
|
{ |
|
52
|
1 |
|
$this->lineIndex++; |
|
53
|
|
|
} |
|
54
|
1 |
|
public function getLineIndex(): int |
|
55
|
|
|
{ |
|
56
|
1 |
|
return $this->lineIndex; |
|
57
|
|
|
} |
|
58
|
1 |
|
public function resetLineIndex() : void |
|
59
|
|
|
{ |
|
60
|
1 |
|
$this->lineIndex = 0; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
public function parseCell(string $attributeName, $content, ?int $entityKey = null): void |
|
64
|
|
|
{ |
|
65
|
2 |
|
$valueSet = $this->getValueSet(); |
|
66
|
2 |
|
$attrSet = $this->getAttributeSet(); |
|
67
|
2 |
|
$attribute = $attrSet->getAttribute($attributeName); |
|
68
|
2 |
|
$value = new Value(); |
|
69
|
2 |
|
$value->setType(ATTR_TYPE::getCase($attribute[_ATTR::TYPE->column()])); |
|
70
|
2 |
|
$value->setValue($content); |
|
71
|
2 |
|
$value->setAttributeKey($attribute[_ATTR::ID->column()]); |
|
72
|
2 |
|
$value->setAttributeName($attribute[_ATTR::NAME->column()]); |
|
73
|
2 |
|
if(!is_null($entityKey)) |
|
74
|
|
|
{ |
|
75
|
1 |
|
$value->setEntityKey($entityKey); |
|
76
|
|
|
} else |
|
77
|
|
|
{ |
|
78
|
1 |
|
$value->setLineIndex($this->getLineIndex()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
$valueSet->appendValue($value); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
4 |
|
public function parseLine(array $line): void |
|
85
|
|
|
{ |
|
86
|
4 |
|
if(!key_exists(_ENTITY::ID->column(), $line)) |
|
87
|
|
|
{ |
|
88
|
1 |
|
EntityException::mustBeEntityKey(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
$entityKey = empty($line[_ENTITY::ID->column()]) |
|
92
|
1 |
|
? null |
|
93
|
2 |
|
: (int) $line[_ENTITY::ID->column()]; |
|
94
|
3 |
|
unset($line[_ENTITY::ID->column()]); |
|
95
|
|
|
|
|
96
|
3 |
|
if ($entityKey < 1) |
|
97
|
|
|
{ |
|
98
|
1 |
|
$this->incrementLineIndex(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
3 |
|
foreach($line as $name => $content) |
|
102
|
|
|
{ |
|
103
|
3 |
|
$this->parseCell($name, $content, $entityKey); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
public function parseChunk(array $chunk): void |
|
108
|
|
|
{ |
|
109
|
1 |
|
foreach ($chunk as $line) |
|
110
|
|
|
{ |
|
111
|
1 |
|
$this->parseLine($line); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
public function createEntities() : array|bool |
|
116
|
|
|
{ |
|
117
|
1 |
|
$model = $this->makeEntityModel(); |
|
118
|
1 |
|
$container = $this->getContainer(); |
|
119
|
|
|
|
|
120
|
1 |
|
$domainKey = $container->getDomainKey(); |
|
121
|
1 |
|
$setKey = $container->getSetKey(); |
|
122
|
|
|
|
|
123
|
1 |
|
$amount = $this->getLineIndex(); |
|
124
|
1 |
|
$serviceKey = $model->getServiceKey(); |
|
125
|
|
|
|
|
126
|
1 |
|
$model->bulkCreate($amount, $domainKey, $setKey, $serviceKey); |
|
127
|
1 |
|
return $model->getByServiceKey($serviceKey); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1 |
|
public function processNewEntities(): void |
|
131
|
|
|
{ |
|
132
|
1 |
|
$valueModel = $this->makeValueModel(); |
|
133
|
1 |
|
$container = $this->getContainer(); |
|
134
|
1 |
|
$valueSet = $this->getValueSet(); |
|
135
|
1 |
|
$domainKey = $container->getDomainKey(); |
|
136
|
1 |
|
$bulkCreateSet = $this->makeBulkValuesSet(); |
|
137
|
1 |
|
$entities = $this->createEntities(); |
|
138
|
|
|
/** |
|
139
|
|
|
* @var Value $value |
|
140
|
|
|
*/ |
|
141
|
1 |
|
foreach($valueSet->forNewEntities() as $value) |
|
142
|
|
|
{ |
|
143
|
1 |
|
if($value->isEmptyValue()) continue; |
|
144
|
1 |
|
$value->setEntityKey($entities[$value->getLineIndex() - 1][_ENTITY::ID->column()]); |
|
145
|
1 |
|
$bulkCreateSet->appendValue($value); |
|
146
|
|
|
} |
|
147
|
1 |
|
$valueModel->bulkCreate($bulkCreateSet, $domainKey); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
2 |
|
public function processExistingEntities(): void |
|
151
|
|
|
{ |
|
152
|
2 |
|
$container = $this->getContainer(); |
|
153
|
2 |
|
$domainKey = $container->getDomainKey(); |
|
154
|
2 |
|
$valueSet = $this->getValueSet(); |
|
155
|
2 |
|
$valueModel = $this->makeValueModel(); |
|
156
|
2 |
|
$valueParser = $this->makeValueParser(); |
|
157
|
|
|
/** |
|
158
|
|
|
* @var Value $value |
|
159
|
|
|
*/ |
|
160
|
2 |
|
foreach($valueSet->forExistingEntities() as $attributeValue) |
|
161
|
|
|
{ |
|
162
|
2 |
|
$value = $attributeValue->getValue(); |
|
163
|
2 |
|
$entityKey = $attributeValue->getEntityKey(); |
|
164
|
2 |
|
$attributeKey = $attributeValue->getAttributeKey(); |
|
165
|
2 |
|
$attributeType = $attributeValue->getType(); |
|
166
|
2 |
|
$valueTable = $attributeType->valueTable(); |
|
167
|
2 |
|
if($value == '') |
|
168
|
1 |
|
$valueModel->destroy($valueTable, $domainKey, $entityKey, $attributeKey); |
|
169
|
|
|
else |
|
170
|
|
|
{ |
|
171
|
2 |
|
$value = $valueParser->parse($attributeType, $value); |
|
172
|
2 |
|
$record = $valueModel->find($valueTable, $domainKey, $entityKey, $attributeKey); |
|
173
|
2 |
|
if($record === false) |
|
174
|
|
|
{ |
|
175
|
1 |
|
$valueModel->create($valueTable, $domainKey, $entityKey, $attributeKey, $value); |
|
176
|
|
|
} else |
|
177
|
|
|
{ |
|
178
|
1 |
|
$valueModel->update($valueTable, $domainKey, $entityKey, $attributeKey, $value); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
1 |
|
public function cleanup(): void |
|
185
|
|
|
{ |
|
186
|
1 |
|
$this->getValueSet()->resetValues(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
1 |
|
public function run(): void |
|
190
|
|
|
{ |
|
191
|
1 |
|
$container = $this->getContainer(); |
|
192
|
1 |
|
$driver = $container->getDriver(); |
|
193
|
1 |
|
$attrSet = $this->getAttributeSet(); |
|
194
|
1 |
|
$attrSet->initialize(); |
|
195
|
1 |
|
while (($chunk = $driver->getChunk()) !== null) |
|
196
|
|
|
{ |
|
197
|
1 |
|
$this->parseChunk($chunk); |
|
198
|
1 |
|
$this->processExistingEntities(); |
|
199
|
1 |
|
$this->processNewEntities(); |
|
200
|
1 |
|
$this->cleanup(); |
|
201
|
1 |
|
$this->resetLineIndex(); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
} |