1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Explicit Architecture POC, |
7
|
|
|
* which is created on top of the Symfony Demo application. |
8
|
|
|
* |
9
|
|
|
* (c) Herberto Graça <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Persistence\Session; |
16
|
|
|
|
17
|
|
|
use Acme\App\Core\Port\Persistence\KeyValueStorageInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Kasper Agg |
22
|
|
|
* @author Herberto Graca <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class KeyValueStorage implements KeyValueStorageInterface |
25
|
|
|
{ |
26
|
|
|
/** @var SessionInterface */ |
27
|
|
|
private $session; |
28
|
|
|
|
29
|
|
|
public function __construct(SessionInterface $session) |
30
|
|
|
{ |
31
|
|
|
$this->session = $session; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function get(string $namespace, string $key): ?string |
35
|
|
|
{ |
36
|
|
|
return $this->session->get($this->createKey($namespace, $key)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function set(string $namespace, string $key, string $value): void |
40
|
|
|
{ |
41
|
|
|
$this->session->set($this->createKey($namespace, $key), $value); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function has(string $namespace, string $key): bool |
45
|
|
|
{ |
46
|
|
|
return $this->session->has($this->createKey($namespace, $key)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function remove(string $namespace, string $key): void |
50
|
|
|
{ |
51
|
|
|
$this->session->remove($this->createKey($namespace, $key)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function createKey(string $namespace, string $key): string |
55
|
|
|
{ |
56
|
|
|
return sprintf('%s_%s', $namespace, $key); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|