Completed
Pull Request — master (#5032)
by Grégoire
03:41
created

SessionFilterPersister   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 51
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 4 1
A set() 0 4 1
A reset() 0 4 1
A buildStorageKey() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[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 Sonata\AdminBundle\Filter\Persister;
15
16
use Symfony\Component\HttpFoundation\Session\SessionInterface;
17
18
/**
19
 * This filter persister is storing filters in session.
20
 * This is the default behavior.
21
 *
22
 * @author Yann Eugoné <[email protected]>
23
 */
24
final class SessionFilterPersister implements FilterPersisterInterface
25
{
26
    /**
27
     * @var SessionInterface
28
     */
29
    private $session;
30
31
    /**
32
     * @param SessionInterface $session
33
     */
34
    public function __construct(SessionInterface $session)
35
    {
36
        $this->session = $session;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function get($adminCode)
43
    {
44
        return $this->session->get($this->buildStorageKey($adminCode), []);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function set($adminCode, array $filters): void
51
    {
52
        $this->session->set($this->buildStorageKey($adminCode), $filters);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function reset($adminCode): void
59
    {
60
        $this->session->remove($this->buildStorageKey($adminCode));
61
    }
62
63
    /**
64
     * Build the session key, under which the filter should be stored for given admin code.
65
     *
66
     * @param string $adminCode The admin code
67
     *
68
     * @return string The storage key
69
     */
70
    private function buildStorageKey($adminCode)
71
    {
72
        return $adminCode.'.filter.parameters';
73
    }
74
}
75