Passed
Push — master ( 984447...7073d8 )
by y
02:06
created

AbstractEntity::_get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 2
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 fields 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
     * Lazy-loads missing fields.
32
     *
33
     * @param string $key
34
     * @return mixed
35
     */
36
    protected function _get (string $key) {
37
        if (!array_key_exists($key, $this->data) and isset($this->data['gid'])) { // can't use hasGid(), inf. loop
38
            $this->reload($key);
39
        }
40
        return parent::_get($key);
41
    }
42
43
    /**
44
     * Adds entities, forcing and flagging a diff if needed.
45
     *
46
     * @param string $key
47
     * @param AbstractEntity[] $entities
48
     * @param bool $force
49
     * @return $this
50
     */
51
    protected function _merge (string $key, array $entities, $force = false) {
52
        if ($force or isset($this->data[$key])) {
53
            foreach ($entities as $entity) {
54
                $this->data[$key][] = $entity;
55
            }
56
            $this->data[$key] = array_values(array_unique($this->data[$key]));
57
            if ($force) {
58
                $this->diff[$key] = true;
59
            }
60
            else {
61
                $this->api->getCache()->add($this);
62
            }
63
        }
64
        return $this;
65
    }
66
67
    /**
68
     * Removes sub-entities without flagging a diff.
69
     *
70
     * @param string $key
71
     * @param AbstractEntity[] $entities
72
     * @return $this
73
     */
74
    protected function _remove (string $key, array $entities) {
75
        if (isset($this->data[$key])) {
76
            $this->data[$key] = array_values(array_diff($this->data[$key], $entities));
77
            $this->api->getCache()->add($this);
78
        }
79
        return $this;
80
    }
81
82
    /**
83
     * Absorbs clean missing data from another instance.
84
     *
85
     * @param AbstractEntity $entity
86
     * @return $this
87
     */
88
    public function merge (AbstractEntity $entity) {
89
        assert($entity->api); // hydrated
90
        foreach ($entity->data as $key => $value) {
91
            if (!array_key_exists($key, $this->data) and !isset($entity->diff[$key])) {
92
                $this->data[$key] = $value;
93
            }
94
        }
95
        return $this;
96
    }
97
98
    /**
99
     * Reloads a specific field, or the whole object.
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->api->getCache()->add($this);
113
        return $this;
114
    }
115
116
}