KeyValueStorage   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 41
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 8 2
A set() 0 4 1
A has() 0 4 1
A remove() 0 6 2
A createKey() 0 4 1
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\InMemory;
16
17
use Acme\App\Core\Port\Persistence\KeyValueStorageInterface;
18
19
/**
20
 * @author Kasper Agg
21
 * @author Herberto Graca <[email protected]>
22
 */
23
final class KeyValueStorage implements KeyValueStorageInterface
24
{
25
    /** @var array */
26
    private $storage;
27
28
    public function __construct(array $data = [])
29
    {
30
        $this->storage = $data;
31
    }
32
33
    public function get(string $namespace, string $key): ?string
34
    {
35
        if (!$this->has($namespace, $key)) {
36
            return null;
37
        }
38
39
        return $this->storage[$this->createKey($namespace, $key)];
40
    }
41
42
    public function set(string $namespace, string $key, string $value): void
43
    {
44
        $this->storage[$this->createKey($namespace, $key)] = $value;
45
    }
46
47
    public function has(string $namespace, string $key): bool
48
    {
49
        return array_key_exists($this->createKey($namespace, $key), $this->storage);
50
    }
51
52
    public function remove(string $namespace, string $key): void
53
    {
54
        if ($this->has($namespace, $key)) {
55
            unset($this->storage[$this->createKey($namespace, $key)]);
56
        }
57
    }
58
59
    private function createKey(string $namespace, string $key): string
60
    {
61
        return sprintf('%s_%s', $namespace, $key);
62
    }
63
}
64