Passed
Push — master ( d3cc39...f2a84f )
by Melech
03:59
created

InMemoryStore   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 21
dl 0
loc 92
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A retrieve() 0 5 1
A filterUsers() 0 16 4
A update() 0 9 2
A __construct() 0 3 1
A getUserViaRetrievalFields() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Auth\Store;
15
16
use Valkyrja\Auth\Data\Retrieval\Contract\Retrieval;
17
use Valkyrja\Auth\Data\Retrieval\RetrievalById;
18
use Valkyrja\Auth\Entity\Contract\User;
19
use Valkyrja\Auth\Exception\InvalidUserException;
20
use Valkyrja\Auth\Store\Contract\Store as Contract;
21
22
/**
23
 * Class InMemoryStore.
24
 *
25
 * @author Melech Mizrachi
26
 *
27
 * @template U of User
28
 *
29
 * @implements Contract<U>
30
 */
31
class InMemoryStore implements Contract
32
{
33
    /**
34
     * InMemoryStore construct.
35
     *
36
     * @param U[] $users The users
37
     */
38
    public function __construct(
39
        protected array $users = [],
40
    ) {
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46
    public function retrieve(Retrieval $retrieval, string $user): User|null
47
    {
48
        $retrievalFields = $retrieval->getRetrievalFields($user);
49
50
        return $this->getUserViaRetrievalFields($retrievalFields);
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function create(User $user): void
57
    {
58
        $this->users[] = clone $user;
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function update(User $user): void
65
    {
66
        $existingUser = $this->retrieve(new RetrievalById($user->getIdValue()), $user::class);
67
68
        if ($existingUser === null) {
69
            throw new InvalidUserException('User does not exist.');
70
        }
71
72
        $existingUser->updateProperties($user->asStorableChangedArray());
73
    }
74
75
    /**
76
     * Filter users via retrieval fields.
77
     *
78
     * @param array<non-empty-string, int|non-empty-string|bool|null> $retrievalFields
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, ...empty-string|bool|null> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, int|non-empty-string|bool|null>.
Loading history...
79
     * @param U                                                       $user
80
     *
81
     * @return bool
82
     */
83
    protected function filterUsers(array $retrievalFields, User $user): bool
84
    {
85
        $match = null;
86
87
        foreach ($retrievalFields as $field => $retrievalField) {
88
            /** @var mixed $value */
89
            $value = $user->__get($field);
90
91
            if ($match === null) {
92
                $match = $value === $retrievalField;
93
            } else {
94
                $match = $match && $value === $retrievalField;
0 ignored issues
show
introduced by
$match is of type null, thus it always evaluated to false.
Loading history...
95
            }
96
        }
97
98
        return $match ?? false;
99
    }
100
101
    /**
102
     * Get user via retrieval fields.
103
     *
104
     * @param array<non-empty-string, int|non-empty-string|bool|null> $retrievalFields
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<non-empty-string, ...empty-string|bool|null> at position 2 could not be parsed: Unknown type name 'non-empty-string' at position 2 in array<non-empty-string, int|non-empty-string|bool|null>.
Loading history...
105
     *
106
     * @return U|null
107
     */
108
    protected function getUserViaRetrievalFields(array $retrievalFields): User|null
109
    {
110
        $users = $this->users;
111
112
        $filteredUsers = array_filter(
113
            $users,
114
            /**
115
             * @param U $user
0 ignored issues
show
Bug introduced by
The type Valkyrja\Auth\Store\U was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
116
             *
117
             * @return bool
118
             */
119
            fn (User $user): bool => $this->filterUsers($retrievalFields, $user)
120
        );
121
122
        return $filteredUsers[0] ?? null;
123
    }
124
}
125