Completed
Push — master ( a2f223...c69a80 )
by
unknown
30s
created

SuperGlobal   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 65
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fetch() 0 9 3
A remove() 0 7 2
A getGlobalName() 0 4 1
A processRemoval() 0 9 2
A registerGlobal() 0 5 1
A set() 0 7 1
1
<?php
2
declare(strict_types=1);
3
namespace Zewa\HTTP;
4
5
use Zewa\Interfaces\HTTP\GlobalInterface;
6
use Zewa\Container;
7
use Zewa\Security;
8
9
abstract class SuperGlobal implements GlobalInterface
10
{
11
    /**
12
     * @var Container
13
     */
14
    private $container;
15
16
    /**
17
     * @var Security
18
     */
19
    protected $security;
20
21 8
    public function __construct(Container $container, Security $security)
22
    {
23 8
        $this->container = $container;
24 8
        $this->security = $security;
25 8
    }
26
27 2
    public function set(string $key, $value)
28
    {
29 2
        $global = $this->getGlobalName();
30 2
        $container = $this->container->get($global);
31 2
        $container[$key] = $value;
32 2
        $this->container->set($global, $container);
33 2
    }
34
35 4
    public function fetch(string $key = null, $default = null)
36
    {
37 4
        $global = $this->getGlobalName();
38 4
        if ($key === null) {
39 1
            return $this->container->get($global);
40
        }
41
42 3
        return $this->container->has($global) ? $this->container->get($global)[$key] ?? $default : null;
43
    }
44
45 1
    public function remove(string $key)
46
    {
47 1
        $global = $this->getGlobalName();
48 1
        if ($this->container->has($global)) {
49 1
            $this->processRemoval($key);
50
        }
51 1
    }
52
53 8
    private function getGlobalName() : string
54
    {
55 8
        return (new \ReflectionClass($this))->getShortName();
56
    }
57
58 1
    private function processRemoval(string $key)
59
    {
60 1
        $global = $this->getGlobalName();
61 1
        $container = $this->container->get($global);
62 1
        if (isset($container[$key])) {
63 1
            unset($container[$key]);
64 1
            $this->container->set($global, $container);
65
        }
66 1
    }
67
68 8
    protected function registerGlobal($value)
69
    {
70 8
        $value = $this->security->normalize($value);
71 8
        $this->container->set($this->getGlobalName(), $value);
72 8
    }
73
}
74