AbstractIdentification   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A getIdentifier() 0 3 1
A getType() 0 3 1
A createIdentityHash() 0 3 1
A equals() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\Doorkeeper\Identification;
6
7
abstract class AbstractIdentification implements IdentificationInterface
8
{
9
    /**
10
     * @var string|int
11
     */
12
    private $identifier;
13
14
    private string $identityHash;
15
16
    private string $type;
17
18
    /**
19
     * @param string|int $identifier
20
     */
21
    final public function __construct($identifier)
22
    {
23
        if (!is_scalar($identifier) || is_bool($identifier)) {
0 ignored issues
show
introduced by
The condition is_scalar($identifier) is always true.
Loading history...
introduced by
The condition is_bool($identifier) is always false.
Loading history...
24
            throw new \InvalidArgumentException('Identifier must be a scalar, alpha numeric value');
25
        }
26
27
        $this->validate($identifier);
28
29
        $this->identifier = $identifier;
30
        $this->type = get_class($this);
31
        $this->identityHash = self::createIdentityHash($this);
32
    }
33
34
    final public function getIdentifier()
35
    {
36
        return $this->identifier;
37
    }
38
39
    final public function getType(): string
40
    {
41
        return $this->type;
42
    }
43
44
    final public function equals(IdentificationInterface $identity): bool
45
    {
46
        return $this->identityHash === self::createIdentityHash($identity);
47
    }
48
49
    /**
50
     * @param string|int|float $value
51
     */
52
    abstract protected function validate($value): void;
53
54
    private static function createIdentityHash(IdentificationInterface $identification): string
55
    {
56
        return md5($identification->getType() . (string)$identification->getIdentifier());
57
    }
58
}
59