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.
Passed
Push — master ( 614fe6...de8815 )
by SignpostMarv
06:49
created

DaftObjectMemoryRepository   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Test Coverage

Coverage 90.57%

Importance

Changes 0
Metric Value
wmc 19
eloc 49
dl 0
loc 144
ccs 48
cts 53
cp 0.9057
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A RecallDaftObject() 0 7 2
A RemoveDaftObjectById() 0 4 2
A ForgetDaftObjectById() 0 4 2
A RememberDaftObject() 0 7 1
A RememberDaftObjectData() 0 10 2
A RecallDaftObjectOrThrow() 0 34 3
A RemoveDaftObjectByHashId() 0 6 2
A RecallDaftObjectFromData() 0 15 3
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
/**
12
* @template T as SuitableForRepositoryType
13
*
14
* @template-extends AbstractDaftObjectRepository<T>
15
*/
16
class DaftObjectMemoryRepository extends AbstractDaftObjectRepository
17
{
18
    const BOOL_DEFAULT_ASSUMEDOESNOTEXIST = false;
19
20
    /**
21
    * {@inheritdoc}
22
    *
23
    * @psalm-param T $object
24
    */
25 4
    public function RememberDaftObject(SuitableForRepositoryType $object) : void
26
    {
27 4
        $hashId = $object::DaftObjectIdHash($object);
28
29 4
        $this->memory[$hashId] = $object;
30
31 4
        $this->RememberDaftObjectData($object);
32 4
    }
33
34 4
    public function ForgetDaftObjectById($id) : void
35
    {
36 4
        $this->ForgetDaftObjectByHashId($this->type::DaftObjectIdValuesHash(
37 4
            is_array($id) ? $id : [$id]
38
        ));
39 4
    }
40
41 4
    public function RemoveDaftObjectById($id) : void
42
    {
43 4
        $this->RemoveDaftObjectByHashId($this->type::DaftObjectIdValuesHash(
44 4
            is_array($id) ? $id : [$id]
45
        ));
46 4
    }
47
48
    /**
49
    * {@inheritdoc}
50
    *
51
    * @psalm-return T|null
52
    */
53 4
    public function RecallDaftObject($id) : ? SuitableForRepositoryType
54
    {
55 4
        $hashId = $this->type::DaftObjectIdValuesHash(
56 4
            is_array($id) ? $id : [$id]
57
        );
58
59 4
        return $this->memory[$hashId] ?? $this->RecallDaftObjectFromData($id);
60
    }
61
62
    /**
63
    * @param scalar|(scalar|array|object|null)[] $id
64
    *
65
    * @psalm-param class-string<T> $type
66
    *
67
    * @psalm-return T
68
    */
69 4
    final public function RecallDaftObjectOrThrow(
70
        $id,
71
        string $type = SuitableForRepositoryType::class
72
    ) : SuitableForRepositoryType {
73 4
        $out = $this->RecallDaftObject($id);
74
75 4
        if (is_null($out)) {
76 4
            throw new DaftObjectNotRecalledException(
77
                'Argument 1 passed to ' .
78
                DaftObjectRepository::class .
79
                '::RecallDaftObjectOrThrow() did not resolve to an instance of ' .
80
                SuitableForRepositoryType::class .
81
                ' from ' .
82 4
                static::class .
83 4
                '::RecallDaftObject()'
84
            );
85 4
        } elseif ( ! is_a($out, $type, true)) {
86
            throw new DaftObjectNotRecalledException(
87
                'Argument 1 passed to ' .
88
                DaftObjectRepository::class .
89
                '::RecallDaftObjectOrThrow() did not resolve to an instance of ' .
90
                $type .
91
                ' from ' .
92
                static::class .
93
                '::RecallDaftObject()'
94
            );
95
        }
96
97
        /**
98
        * @psalm-var T
99
        */
100 4
        $out = $out;
101
102 4
        return $out;
103
    }
104
105
    /**
106
    * {@inheritdoc}
107
    *
108
    * @psalm-param T $object
109
    */
110 4
    public function RememberDaftObjectData(
111
        SuitableForRepositoryType $object,
112
        bool $assumeDoesNotExist = self::BOOL_DEFAULT_ASSUMEDOESNOTEXIST
113
    ) : void {
114 4
        $hashId = $object::DaftObjectIdHash($object);
115
116 4
        $this->data[$hashId] = [];
117
118 4
        foreach ($object::DaftObjectPublicGetters() as $property) {
119 4
            $this->data[$hashId][$property] = $object->__get($property);
120
        }
121 4
    }
122
123
    /**
124
    * Recalls the corresponding daft object instance from cached data.
125
    *
126
    * @param scalar|(scalar|array|object|null)[] $id
127
    *
128
    * @psalm-return T|null
129
    */
130 8
    protected function RecallDaftObjectFromData($id) : ? SuitableForRepositoryType
131
    {
132 8
        $hashId = $this->type::DaftObjectIdValuesHash(
133 8
            is_array($id) ? $id : [$id]
134
        );
135
136 8
        if (isset($this->data[$hashId])) {
137 4
            $type = $this->type;
138
139 4
            $out = new $type($this->data[$hashId]);
140
141 4
            return $out;
142
        }
143
144 8
        return null;
145
    }
146
147 4
    private function ForgetDaftObjectByHashId(string $hashId) : void
148
    {
149 4
        if (true === isset($this->memory[$hashId])) {
150 4
            unset($this->memory[$hashId]);
151
        }
152 4
    }
153
154 4
    private function RemoveDaftObjectByHashId(string $hashId) : void
155
    {
156 4
        $this->ForgetDaftObjectByHashId($hashId);
157
158 4
        if (true === isset($this->data[$hashId])) {
159 4
            unset($this->data[$hashId]);
160
        }
161 4
    }
162
}
163