AbstractService   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 29
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isCsrfTokenValid() 0 3 1
A __construct() 0 4 1
A addFlash() 0 3 1
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