Passed
Push — master ( 7073d8...993609 )
by y
01:42
created

AbstractEntity::getCacheKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helix\Asana\Base;
4
5
/**
6
 * A resource with a GID.
7
 *
8
 * @method string   getGid          ()
9
 * @method bool     hasGid          ()
10
 * @method string   getResourceType ()
11
 */
12
abstract class AbstractEntity extends Data {
13
14
    /**
15
     * GET/PUT/DELETE path.
16
     *
17
     * @return string
18
     */
19
    abstract public function __toString (): string;
20
21
    /**
22
     * Maps lazy-loaded / reloadable mapped {@see Data} to their proper expanded field expression.
23
     *
24
     * If not set here, the field name is used alone.
25
     *
26
     * @var string[]
27
     */
28
    protected static $optFields = [];
29
30
    /**
31
     * Shortcut to update the cache.
32
     */
33
    protected function _cache (): void {
34
        $this->api->getCache()->add($this);
35
    }
36
37
    /**
38
     * Lazy-loads missing fields.
39
     *
40
     * @param string $key
41
     * @return mixed
42
     */
43
    protected function _get (string $key) {
44
        if (!array_key_exists($key, $this->data) and isset($this->data['gid'])) { // can't use hasGid(), inf. loop
45
            $this->reload($key);
46
        }
47
        return parent::_get($key);
48
    }
49
50
    /**
51
     * Adds entities, forcing and flagging a diff if needed.
52
     *
53
     * @param string $key
54
     * @param AbstractEntity[] $entities
55
     * @param bool $force
56
     * @return $this
57
     */
58
    protected function _merge (string $key, array $entities, $force = false) {
59
        if ($force or isset($this->data[$key])) {
60
            foreach ($entities as $entity) {
61
                $this->data[$key][] = $entity;
62
            }
63
            $this->data[$key] = array_values(array_unique($this->data[$key]));
64
            if ($force) {
65
                $this->diff[$key] = true;
66
            }
67
            else {
68
                $this->_cache();
69
            }
70
        }
71
        return $this;
72
    }
73
74
    /**
75
     * Removes sub-entities without flagging a diff.
76
     *
77
     * @param string $key
78
     * @param AbstractEntity[] $entities
79
     * @return $this
80
     */
81
    protected function _remove (string $key, array $entities) {
82
        if (isset($this->data[$key])) {
83
            $this->data[$key] = array_values(array_diff($this->data[$key], $entities));
84
            $this->_cache();
85
        }
86
        return $this;
87
    }
88
89
    /**
90
     * Cache keys for the entity.
91
     *
92
     * @return string[]
93
     */
94
    public function getCacheKeys () {
95
        return [$this->getGid(), (string)$this];
96
    }
97
98
    /**
99
     * Reloads a specific field, or the whole entity.
100
     *
101
     * @param string $key
102
     * @return $this
103
     */
104
    public function reload (string $key = null) {
105
        if (isset($key)) {
106
            $value = $this->api->get($this, [], ['fields' => static::$optFields[$key] ?? $key])[$key] ?? null;
107
            $this->_setMapped($key, $value);
108
        }
109
        else {
110
            $this->_setData($this->api->get($this, [], ['expand' => 'this']));
1 ignored issue
show
Bug introduced by
It seems like $this->api->get($this, a...ay('expand' => 'this')) can also be of type null; however, parameter $data of Helix\Asana\Base\Data::_setData() does only seem to accept array, 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

110
            $this->_setData(/** @scrutinizer ignore-type */ $this->api->get($this, [], ['expand' => 'this']));
Loading history...
111
        }
112
        $this->_cache();
113
        return $this;
114
    }
115
116
}