Completed
Push — master ( 7976ca...734e62 )
by Mārtiņš
01:25
created

Identity   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 80
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A define() 0 12 3
A load() 0 6 1
A save() 0 6 1
A delete() 0 6 1
A has() 0 6 1
A computeKey() 0 14 3
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\EmailIdentity::class         => Mapper\EmailIdentity::class,
16
        Entity\CookieIdentity::class        => Mapper\CookieIdentity::class,
17
        Entity\NonceIdentity::class         => Mapper\NonceIdentity::class,
18
        Entity\IdentityCollection::class    => Mapper\IdentityCollection::class,
19
        'IdentityAccount'                   => Mapper\IdentityAccount::class,
20
    ];
21
22
23
    public function __construct(CanCreateMapper $mapperFactory)
24
    {
25
        $this->mapperFactory = $mapperFactory;
0 ignored issues
show
Bug introduced by
The property mapperFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
    }
27
28
29
    public function define(string $entity, string $mapper)
30
    {
31
        if (class_exists($entity) === false) {
32
            throw new RuntimeException("Class '{$entity}' was not found!");
33
        }
34
35
        if (class_exists($mapper) === false) {
36
            throw new RuntimeException("Class '{$mapper}' was not found!");
37
        }
38
39
        $this->list[$entity] = $mapper;
40
    }
41
42
43
44
    public function load($identity, string $override = null)
45
    {
46
        $key = $this->computeKey(get_class($identity), $override);
47
        $mapper = $this->mapperFactory->create($this->list[$key]);
48
        $mapper->fetch($identity);
49
    }
50
51
52
    public function save($identity, string $override = null)
53
    {
54
        $key = $this->computeKey(get_class($identity), $override);
55
        $mapper = $this->mapperFactory->create($this->list[$key]);
56
        $mapper->store($identity);
57
    }
58
59
60
    public function delete($identity, string $override = null)
61
    {
62
        $key = $this->computeKey(get_class($identity), $override);
63
        $mapper = $this->mapperFactory->create($this->list[$key]);
64
        $mapper->remove($identity);
65
    }
66
67
68
    public function has($identity, string $override = null)
69
    {
70
        $key = $this->computeKey(get_class($identity), $override);
71
        $mapper = $this->mapperFactory->create($this->list[$key]);
72
        return $mapper->exists($identity);
73
    }
74
75
76
    private function computeKey(string $key, string $override = null)
77
    {
78
        if ($override !== null) {
79
            $key = $override;
80
        }
81
82
        // var_dump([$key, $this->list]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
84
        if (array_key_exists($key, $this->list) === false) {
85
            throw new RuntimeException("No mapper for class '{$key}' has been defined!");
86
        }
87
88
        return $key;
89
    }
90
}
91