Completed
Push — master ( 87411d...dfec5f )
by Mārtiņš
02:21
created

Identity::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Palladium\Repository;
4
5
use Palladium\Entity;
6
use Palladium\Mapper;
7
use Palladium\Contract\CanCreateMapper;
8
use RuntimeException;
9
10
11
class Identity
12
{
13
    private $list = [
14
        Entity\Identity::class              => Mapper\Identity::class,
15
        Entity\StandardIdentity::class      => Mapper\StandardIdentity::class,
16
        Entity\CookieIdentity::class        => Mapper\CookieIdentity::class,
17
        Entity\NonceIdentity::class         => Mapper\NonceIdentity::class,
18
        Entity\IdentityCollection::class    => Mapper\IdentityCollection::class,
19
    ];
20
21
    private $mapperFactory;
22
23 7
    public function __construct(CanCreateMapper $mapperFactory)
24
    {
25 7
        $this->mapperFactory = $mapperFactory;
26 7
    }
27
28
29 7
    public function define(string $entity, string $mapper)
30
    {
31 7
        if (class_exists($entity) === false) {
32 1
            throw new RuntimeException("Class '{$entity}' was not found!");
33
        }
34
35 6
        if (class_exists($mapper) === false) {
36 1
            throw new RuntimeException("Class '{$mapper}' was not found!");
37
        }
38
39 5
        $this->list[$entity] = $mapper;
40 5
    }
41
42
43
44 2
    public function load($identity, string $override = null)
45
    {
46 2
        $key = $this->computeKey(get_class($identity), $override);
47 1
        $mapper = $this->mapperFactory->create($this->list[$key]);
48 1
        $mapper->fetch($identity);
49 1
    }
50
51
52 1
    public function save($identity, string $override = null)
53
    {
54 1
        $key = $this->computeKey(get_class($identity), $override);
55 1
        $mapper = $this->mapperFactory->create($this->list[$key]);
56 1
        $mapper->store($identity);
57 1
    }
58
59
60 1
    public function delete($identity, string $override = null)
61
    {
62 1
        $key = $this->computeKey(get_class($identity), $override);
63 1
        $mapper = $this->mapperFactory->create($this->list[$key]);
64 1
        $mapper->remove($identity);
65 1
    }
66
67
68 1
    public function has($identity, string $override = null)
69
    {
70 1
        $key = $this->computeKey(get_class($identity), $override);
71 1
        $mapper = $this->mapperFactory->create($this->list[$key]);
72 1
        return $mapper->exists($identity);
73
    }
74
75
76 5
    private function computeKey(string $key, string $override = null)
77
    {
78 5
        if ($override !== null) {
79 1
            $key = $override;
80
        }
81
82 5
        if (array_key_exists($key, $this->list) === false) {
83 1
            throw new RuntimeException("No mapper for class '{$key}' has been defined!");
84
        }
85
86 4
        return $key;
87
    }
88
}
89