AbstractService::isCsrfTokenValid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service;
6
7
use App\Service\Cache\ClearCache;
8
use Symfony\Component\HttpFoundation\RequestStack;
9
use Symfony\Component\HttpFoundation\Session\SessionInterface;
10
use Symfony\Component\Security\Csrf\CsrfToken;
11
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
12
13
abstract class AbstractService
14
{
15
    use ClearCache;
16
17
    private SessionInterface $session;
18
    private CsrfTokenManagerInterface $tokenManager;
19
20
    public function __construct(CsrfTokenManagerInterface $tokenManager, RequestStack $requestStack)
21
    {
22
        $this->tokenManager = $tokenManager;
23
        $this->session = $requestStack->getSession();
24
    }
25
26
    /**
27
     * Checks the validity of a CSRF token.
28
     */
29
    protected function isCsrfTokenValid(string $id, ?string $token): bool
30
    {
31
        return $this->tokenManager->isTokenValid(new CsrfToken($id, $token));
32
    }
33
34
    /**
35
     * Adds a flash message to the current session for type.
36
     *
37
     * @throws \LogicException
38
     */
39
    protected function addFlash(string $type, string $message): void
40
    {
41
        $this->session->getFlashBag()->add($type, $message);
42
    }
43
}
44