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

SessionAuthenticator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
dl 0
loc 45
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthenticatedUsersFromSession() 0 18 3
A __construct() 0 13 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;
15
16
use Valkyrja\Auth\Constant\SessionId;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Auth\Constant\SessionId 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...
17
use Valkyrja\Auth\Data\Contract\AuthenticatedUsers;
18
use Valkyrja\Auth\Entity\Contract\User;
19
use Valkyrja\Auth\Hasher\Contract\PasswordHasher;
20
use Valkyrja\Auth\Store\Contract\Store;
21
use Valkyrja\Session\Contract\Session;
22
23
use function is_string;
24
25
/**
26
 * Class SessionAuthenticator.
27
 *
28
 * @author Melech Mizrachi
29
 *
30
 * @template U of User
31
 *
32
 * @extends AbstractAuthenticator<U>
33
 */
34
class SessionAuthenticator extends AbstractAuthenticator
35
{
36
    /**
37
     * @param Store<U>        $store  The store
38
     * @param class-string<U> $entity The user entity
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<U> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<U>.
Loading history...
39
     */
40
    public function __construct(
41
        protected Session $session,
42
        Store $store,
43
        PasswordHasher $hasher,
44
        string $entity,
45
        AuthenticatedUsers|null $authenticatedUsers = null,
46
        protected string $sessionId = SessionId::AUTHENTICATED_USERS,
47
    ) {
48
        parent::__construct(
49
            store: $store,
50
            hasher: $hasher,
51
            entity: $entity,
52
            authenticatedUsers: $authenticatedUsers ?? $this->getAuthenticatedUsersFromSession() ?? new Data\AuthenticatedUsers(),
53
        );
54
    }
55
56
    /**
57
     * Attempt to get the authenticated users from the session.
58
     *
59
     * @return AuthenticatedUsers|null
60
     */
61
    protected function getAuthenticatedUsersFromSession(): AuthenticatedUsers|null
62
    {
63
        $sessionSerializedUsers = $this->session->get($this->sessionId);
64
65
        if (! is_string($sessionSerializedUsers)) {
66
            return null;
67
        }
68
69
        $sessionUsers = unserialize(
70
            $sessionSerializedUsers,
71
            ['allowed_classes' => true]
72
        );
73
74
        if (! $sessionUsers instanceof AuthenticatedUsers) {
75
            return null;
76
        }
77
78
        return $sessionUsers;
79
    }
80
}
81