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

OrmStore   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
dl 0
loc 47
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A retrieve() 0 17 2
A create() 0 5 1
A update() 0 5 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\Entity\Contract\User;
18
use Valkyrja\Auth\Store\Contract\Store as Contract;
19
use Valkyrja\Orm\Contract\Manager;
20
use Valkyrja\Orm\Data\Value;
21
use Valkyrja\Orm\Data\Where;
22
use Valkyrja\Orm\Repository\Contract\Repository;
23
24
/**
25
 * Class OrmStore.
26
 *
27
 * @author Melech Mizrachi
28
 *
29
 * @template U of User
30
 *
31
 * @implements Contract<U>
32
 */
33
class OrmStore implements Contract
34
{
35
    public function __construct(
36
        protected Manager $orm,
37
    ) {
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function retrieve(Retrieval $retrieval, string $user): User|null
44
    {
45
        /** @var Repository<U> $repository */
46
        $repository = $this->orm->createRepository($user);
47
48
        $where = [];
49
50
        foreach ($retrieval->getRetrievalFields($user) as $field => $value) {
51
            $where[] = new Where(
52
                value: new Value(
53
                    name: $field,
54
                    value: $value
55
                )
56
            );
57
        }
58
59
        return $repository->findBy(...$where);
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function create(User $user): void
66
    {
67
        $repository = $this->orm->createRepository($user::class);
68
69
        $repository->create($user);
70
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75
    public function update(User $user): void
76
    {
77
        $repository = $this->orm->createRepository($user::class);
78
79
        $repository->update($user);
80
    }
81
}
82