GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 53bcfa...6873de )
by SignpostMarv
03:59
created

DaftObjectMemoryRepository   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 117
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A RecallDaftObject() 0 9 2
A ForgetDaftObjectById() 0 3 1
A RememberDaftObjectData() 0 9 2
A RemoveDaftObjectById() 0 3 1
A RemoveDaftObjectByHashId() 0 6 2
A RememberDaftObject() 0 9 1
A ObjectHashId() 0 10 2
A RecallDaftObjectFromData() 0 15 2
A ForgetDaftObjectByHashId() 0 4 2
1
<?php
2
/**
3
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject;
10
11
class DaftObjectMemoryRepository extends AbstractDaftObjectRepository
12
{
13
    /**
14
    * @var DefinesOwnIdPropertiesInterface[]
15
    */
16
    protected $memory = [];
17
18
    /**
19
    * mixed[][].
20
    */
21
    protected $data = [];
22
23 6
    public function RememberDaftObject(DefinesOwnIdPropertiesInterface $object) : void
24
    {
25 6
        static::ThrowIfNotType($object, $this->type, 1, __FUNCTION__);
26
27 4
        $hashId = $object::DaftObjectIdHash($object);
28
29 4
        $this->memory[$hashId] = $object;
30
31 4
        $this->RememberDaftObjectData($object);
32 4
    }
33
34
    /**
35
    * @param mixed $id
36
    */
37 4
    public function ForgetDaftObjectById($id) : void
38
    {
39 4
        $this->ForgetDaftObjectByHashId($this->ObjectHashId($id));
40 4
    }
41
42
    /**
43
    * @param mixed $id
44
    */
45 4
    public function RemoveDaftObjectById($id) : void
46
    {
47 4
        $this->RemoveDaftObjectByHashId($this->ObjectHashId($id));
48 4
    }
49
50
    /**
51
    * @param mixed $id
52
    */
53 4
    public function RecallDaftObject($id) : ? DaftObject
54
    {
55 4
        $hashId = $this->ObjectHashId($id);
56
57 4
        if (false === isset($this->memory[$hashId])) {
58 4
            return $this->RecallDaftObjectFromData($id);
59
        }
60
61 4
        return $this->memory[$hashId];
62
    }
63
64 4
    protected function RememberDaftObjectData(DefinesOwnIdPropertiesInterface $object) : void
65
    {
66 4
        $hashId = $object::DaftObjectIdHash($object);
67
68 4
        $this->data[$hashId] = [];
69
70 4
        foreach ($object::DaftObjectPublicGetters() as $property) {
71 4
            $getter = 'Get' . ucfirst($property);
72 4
            $this->data[$hashId][$property] = $object->$getter();
73
        }
74 4
    }
75
76
    /**
77
    * Recalls the corresponding daft object instance from cached data.
78
    *
79
    * @param mixed $id
80
    */
81 4
    protected function RecallDaftObjectFromData($id) : ? DaftObject
82
    {
83 4
        $hashId = $this->ObjectHashId($id);
84 4
        if (true === isset($this->data[$hashId])) {
85 4
            $type = $this->type;
86
87
            /**
88
            * @var DaftObject $out
89
            */
90 4
            $out = new $type($this->data[$hashId]);
91
92 4
            return $out;
93
        }
94
95 4
        return null;
96
    }
97
98
    /**
99
    * Converts an id to a unique-enough-for-now string.
100
    *
101
    * @param mixed $id
102
    */
103 4
    private function ObjectHashId($id) : string
104
    {
105 4
        $id = is_array($id) ? $id : [$id];
106
107
        /**
108
        * @var DefinesOwnIdPropertiesInterface $type
109
        */
110 4
        $type = $this->type;
111
112 4
        return $type::DaftObjectIdValuesHash($id);
113
    }
114
115 4
    private function ForgetDaftObjectByHashId(string $hashId) : void
116
    {
117 4
        if (true === isset($this->memory[$hashId])) {
118 4
            unset($this->memory[$hashId]);
119
        }
120 4
    }
121
122 4
    private function RemoveDaftObjectByHashId(string $hashId) : void
123
    {
124 4
        $this->ForgetDaftObjectByHashId($hashId);
125
126 4
        if (true === isset($this->data[$hashId])) {
127 4
            unset($this->data[$hashId]);
128
        }
129 4
    }
130
}
131