Identity::load()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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