Passed
Push — master ( e16666...667470 )
by Filipe
01:49 queued 13s
created

SlickApp::generator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of template
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\Template\Extension;
13
14
use Psr\Http\Message\ServerRequestInterface;
15
use Slick\Configuration\ConfigurationInterface;
16
use Slick\Configuration\Driver\Environment;
17
use Slick\WebStack\Domain\Security\AuthorizationCheckerInterface;
18
use Slick\WebStack\Domain\Security\SecurityAuthenticatorInterface;
19
use Slick\WebStack\Domain\Security\UserInterface;
20
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
21
22
/**
23
 * SlickApp
24
 *
25
 * @package Slick\Template\Extension
26
 */
27
class SlickApp
28
{
29
    /**
30
     * Creates a SlickApp
31
     *
32
     * @param AuthorizationCheckerInterface<UserInterface>|null $auth
33
     * @param SecurityAuthenticatorInterface|null $authenticator
34
     * @param ServerRequestInterface|null $request
35
     * @param ConfigurationInterface|null $settings
36
     */
37
    public function __construct(
38
        private readonly ?AuthorizationCheckerInterface $auth = null,
39
        private readonly ?SecurityAuthenticatorInterface $authenticator = null,
40
        private readonly ?ServerRequestInterface $request = null,
41
        private readonly ?ConfigurationInterface $settings = null,
42
        private readonly ?UrlGeneratorInterface $generator = null
43
    ) {
44
    }
45
46
    /**
47
     * Retrieves the authenticated user, if available.
48
     *
49
     * @return UserInterface|null The authenticated user, or null if not authenticated.
50
     */
51
    public function user(): ?UserInterface
52
    {
53
        $enabled = $this->authenticator && $this->authenticator->enabled();
54
        if ($enabled && $this->auth instanceof AuthorizationCheckerInterface) {
55
            return $this->auth->authenticatedUser();
56
        }
57
        return null;
58
    }
59
60
    /**
61
     * Retrieve the server request.
62
     *
63
     * @return ServerRequestInterface|null The server request object, or null if not set.
64
     */
65
    public function request(): ?ServerRequestInterface
66
    {
67
        return $this->request;
68
    }
69
70
    public function settings(): ConfigurationInterface
71
    {
72
        if (!$this->settings) {
73
            return new Environment();
74
        }
75
        return $this->settings;
76
    }
77
78
    public function generator(): ?UrlGeneratorInterface
79
    {
80
        return $this->generator;
81
    }
82
}
83