Passed
Push — master ( 199ece...6bc7da )
by y
02:13
created

Pool::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
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
     * @var AbstractEntity[]
14
     */
15
    protected $entities = [];
16
17
    /**
18
     * @var string[]
19
     */
20
    protected $ids = [];
21
22
    protected function _add (AbstractEntity $entity): void {
23
        assert($entity->hasId());
24
        $this->entities[$entity->getId()] = $entity;
25
    }
26
27
    /**
28
     * @param AbstractEntity $entity
29
     * @param string[] $keys
30
     */
31
    protected function _addKeys (AbstractEntity $entity, ...$keys): void {
32
        assert($entity->hasId());
33
        $this->ids += array_fill_keys($keys, $entity->getId());
34
    }
35
36
    /**
37
     * @param string $key
38
     * @param Api|Data $caller
39
     * @return null|AbstractEntity
40
     */
41
    protected function _get (string $key, $caller) {
42
        if (isset($this->ids[$key])) {
43
            return $this->entities[$this->ids[$key]];
44
        }
45
        unset($caller);
46
        return null;
47
    }
48
49
    final public function add (AbstractEntity $entity): void {
50
        if ($entity->hasId() and !$entity->isDiff()) {
51
            $this->_add($entity);
52
            $this->_addKeys($entity, $entity->getPoolKeys());
0 ignored issues
show
Bug introduced by
$entity->getPoolKeys() of type string[] is incompatible with the type string expected by parameter $keys of Helix\Shopify\Api\Pool::_addKeys(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
            $this->_addKeys($entity, /** @scrutinizer ignore-type */ $entity->getPoolKeys());
Loading history...
53
        }
54
    }
55
56
    /**
57
     * @param string $key
58
     * @param Api|Data $caller
59
     * @param Closure $factory `fn( $caller ): ?AbstractEntity`
60
     * @return null|AbstractEntity
61
     */
62
    final public function get (string $key, $caller, Closure $factory) {
63
        /** @var AbstractEntity $entity */
64
        if (!$entity = $this->_get($key, $caller) and $entity = $factory($caller)) {
65
            $id = $entity->getId();
66
            if ($this->has($id) and $pooled = $this->_get($id, $caller)) {
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type null; however, parameter $key of Helix\Shopify\Api\Pool::_get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            if ($this->has($id) and $pooled = $this->_get(/** @scrutinizer ignore-type */ $id, $caller)) {
Loading history...
Bug introduced by
It seems like $id can also be of type null; however, parameter $key of Helix\Shopify\Api\Pool::has() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
            if ($this->has(/** @scrutinizer ignore-type */ $id) and $pooled = $this->_get($id, $caller)) {
Loading history...
67
                if ($pooled->__merge($entity)) { // new data?
68
                    $this->add($pooled); // renew everything
69
                }
70
                $this->_addKeys($pooled, $key);
71
                return $pooled;
72
            }
73
            $this->add($entity);
74
            $this->_addKeys($entity, $key);
75
        }
76
        return $entity;
77
    }
78
79
    /**
80
     * Polls.
81
     *
82
     * @param string $key
83
     * @return bool
84
     */
85
    public function has (string $key): bool {
86
        return isset($this->ids[$key]);
87
    }
88
89
    /**
90
     * @param string[] $keys
91
     */
92
    public function remove (...$keys): void {
93
        foreach ($keys as $key) {
94
            unset($this->entities[$key]);
95
            unset($this->ids[$key]);
96
        }
97
    }
98
}