|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helix\Shopify\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Helix\Shopify\Api; |
|
7
|
|
|
use Helix\Shopify\Base\AbstractEntity; |
|
8
|
|
|
use Helix\Shopify\Base\Data; |
|
9
|
|
|
|
|
10
|
|
|
class Pool |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var AbstractEntity[] |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $entities = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string[] |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $ids = []; |
|
22
|
|
|
|
|
23
|
|
|
protected function _add(AbstractEntity $entity): void |
|
24
|
|
|
{ |
|
25
|
|
|
assert($entity->hasId()); |
|
26
|
|
|
$this->entities[$entity->getId()] = $entity; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param AbstractEntity $entity |
|
31
|
|
|
* @param string[] $keys |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function _addKeys(AbstractEntity $entity, ...$keys): void |
|
34
|
|
|
{ |
|
35
|
|
|
assert($entity->hasId()); |
|
36
|
|
|
$this->ids += array_fill_keys($keys, $entity->getId()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $key |
|
41
|
|
|
* @param Api|Data $caller |
|
42
|
|
|
* @return null|AbstractEntity |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function _get(string $key, $caller) |
|
45
|
|
|
{ |
|
46
|
|
|
if (isset($this->ids[$key])) { |
|
47
|
|
|
return $this->entities[$this->ids[$key]]; |
|
48
|
|
|
} |
|
49
|
|
|
unset($caller); |
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
final public function add(AbstractEntity $entity): void |
|
54
|
|
|
{ |
|
55
|
|
|
if ($entity->hasId() and !$entity->isDiff()) { |
|
56
|
|
|
$this->_add($entity); |
|
57
|
|
|
$this->_addKeys($entity, ...$entity->getPoolKeys()); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $key |
|
63
|
|
|
* @param Api|Data $caller |
|
64
|
|
|
* @param Closure $factory `fn( $caller ): ?AbstractEntity` |
|
65
|
|
|
* @return null|AbstractEntity |
|
66
|
|
|
*/ |
|
67
|
|
|
final public function get(string $key, $caller, Closure $factory) |
|
68
|
|
|
{ |
|
69
|
|
|
/** @var AbstractEntity $entity */ |
|
70
|
|
|
if (!$entity = $this->_get($key, $caller) and $entity = $factory($caller)) { |
|
71
|
|
|
$id = $entity->getId(); |
|
72
|
|
|
if ($this->has($id) and $pooled = $this->_get($id, $caller)) { |
|
|
|
|
|
|
73
|
|
|
if ($pooled->__merge($entity)) { // new data? |
|
74
|
|
|
$this->add($pooled); // renew everything |
|
75
|
|
|
} |
|
76
|
|
|
$this->_addKeys($pooled, $key); |
|
77
|
|
|
return $pooled; |
|
78
|
|
|
} |
|
79
|
|
|
$this->add($entity); |
|
80
|
|
|
$this->_addKeys($entity, $key); |
|
81
|
|
|
} |
|
82
|
|
|
return $entity; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Polls. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $key |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function has(string $key): bool |
|
92
|
|
|
{ |
|
93
|
|
|
return isset($this->ids[$key]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string[] $keys |
|
98
|
|
|
*/ |
|
99
|
|
|
public function remove(...$keys): void |
|
100
|
|
|
{ |
|
101
|
|
|
foreach ($keys as $key) { |
|
102
|
|
|
unset($this->entities[$key]); |
|
103
|
|
|
unset($this->ids[$key]); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |