Passed
Branch master (372b2a)
by Filipe
01:32
created

ProfileFactoryTrait::createPasswordHasher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of web-stack
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\WebStack\Domain\Security\Http;
13
14
use Slick\WebStack\Domain\Security\Authentication\Token\TokenStorageInterface;
15
use Slick\WebStack\Domain\Security\PasswordHasher\Hasher\PhpPasswordHasher;
16
use Slick\WebStack\Domain\Security\PasswordHasher\PasswordHasherInterface;
17
use Slick\WebStack\Domain\Security\User\UserProviderInterface;
18
use Slick\WebStack\Domain\Security\UserInterface;
19
use Psr\Container\ContainerExceptionInterface;
20
use Psr\Container\NotFoundExceptionInterface;
21
use Slick\Http\Session\SessionDriverInterface;
22
23
/**
24
 * ProfileFactoryTrait
25
 *
26
 * @package Slick\WebStack\Domain\Security\Http
27
 * @template-covariant TUser of UserInterface
28
 */
29
trait ProfileFactoryTrait
30
{
31
32
    /** @var array<string, mixed>  */
33
    protected static array $defaultOptions = [
34
        "secured" => true,
35
        "passwordHasher" => PhpPasswordHasher::class,
36
        "authenticators" => [],
37
        "stateless" => true,
38
        "tokenStorage" => TokenStorageInterface::class,
39
        "sessionDriver" => SessionDriverInterface::class,
40
        "entryPoint" => null
41
    ];
42
43
    /**
44
     * Create a session driver based on a given profile modules.
45
     *
46
     * @param array<string, mixed> $profile The profile configuration.
47
     * @return SessionDriverInterface The created session driver.
48
     *
49
     * @throws ContainerExceptionInterface
50
     * @throws NotFoundExceptionInterface
51
     */
52
    private function createSessionDriver(array $profile): SessionDriverInterface
53
    {
54
        $config = array_merge(self::$defaultOptions, $profile);
55
        return $this->container->get($config['sessionDriver']);
56
    }
57
58
    /**
59
     * Create a token storage based on a given profile.
60
     *
61
     * @param array<string, mixed> $profile The profile configuration.
62
     * @return TokenStorageInterface<UserInterface> The created token storage.
63
     * @throws ContainerExceptionInterface
64
     * @throws NotFoundExceptionInterface
65
     */
66
    private function createTokenStorage(array $profile): TokenStorageInterface
67
    {
68
        $config = array_merge(self::$defaultOptions, $profile);
69
        return $this->container->get($config['tokenStorage']);
70
    }
71
72
    /**
73
     * Creates a password hasher.
74
     *
75
     * @param array<string, mixed> $profile The profile array containing the configuration options.
76
     * @return PasswordHasherInterface The created password hasher.
77
     * @throws ContainerExceptionInterface
78
     * @throws NotFoundExceptionInterface
79
     */
80
    private function createPasswordHasher(array $profile): PasswordHasherInterface
81
    {
82
        $config = array_merge(self::$defaultOptions, $profile);
83
        return $this->container->get($config['passwordHasher']);
84
    }
85
86
    /**
87
     * Creates a user provider instance based on a given provider class.
88
     *
89
     * @param string $providerClass The class name of the user provider
90
     *
91
     * @return UserProviderInterface<UserInterface> The user provider instance
92
     *
93
     * @throws ContainerExceptionInterface
94
     * @throws NotFoundExceptionInterface
95
     */
96
    private function createUserProvider(string $providerClass): UserProviderInterface
97
    {
98
        return $this->container->get($providerClass);
99
    }
100
}
101