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

SessionFilterPersister::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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