Requestor::hasIdentification()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\Doorkeeper;
6
7
use Psr\Http;
8
use RemotelyLiving\Doorkeeper\Identification;
9
10
final class Requestor implements RequestorInterface
11
{
12
    /**
13
     * @var Identification\Collection[]
14
     */
15
    private $idCollections = [];
16
17
    public function __construct(array $identifications = [])
18
    {
19
        foreach ($identifications as $identification) {
20
            $this->registerIdentification($identification);
21
        }
22
    }
23
24
    public function getIdentityHash(): string
25
    {
26
        return md5(serialize($this->idCollections));
27
    }
28
29
    public function registerIdentification(Identification\IdentificationInterface $identification): void
30
    {
31
        $type = $identification->getType();
32
33
        if (!isset($this->idCollections[$type])) {
34
            $this->idCollections[$type] = new Identification\Collection($type, [$identification]);
35
            return;
36
        }
37
38
        $this->idCollections[$type]->add($identification);
39
    }
40
41
    public function getIdentificationCollections(): array
42
    {
43
        return $this->idCollections;
44
    }
45
46
    public function hasIdentification(Identification\IdentificationInterface $identification): bool
47
    {
48
        if (!isset($this->idCollections[$identification->getType()])) {
49
            return false;
50
        }
51
52
        return $this->idCollections[$identification->getType()]->has($identification);
53
    }
54
55
    /**
56
     * @param string|int $id
57
     */
58
    public function withUserId($id): self
59
    {
60
        $mutee = clone $this;
61
        $mutee->registerIdentification(new Identification\UserId($id));
62
63
        return $mutee;
64
    }
65
66
    public function withIpAddress(string $ipAddress): self
67
    {
68
        $mutee = clone $this;
69
        $mutee->registerIdentification(new Identification\IpAddress($ipAddress));
70
71
        return $mutee;
72
    }
73
74
    public function withStringHash(string $hash): self
75
    {
76
        $mutee = clone $this;
77
        $mutee->registerIdentification(new Identification\StringHash($hash));
78
79
        return $mutee;
80
    }
81
82
    public function withRequest(Http\Message\RequestInterface $request): self
83
    {
84
        $mutee = clone $this;
85
        $mutee->registerIdentification(Identification\HttpHeader::createFromRequest($request));
86
87
        return $mutee;
88
    }
89
90
    public function withEnvironment(string $environment): self
91
    {
92
        $mutee = clone $this;
93
        $mutee->registerIdentification(new Identification\Environment($environment));
94
95
        return $mutee;
96
    }
97
98
    public function withPipedComposite(string $pipedComposite): self
99
    {
100
        $mutee = clone $this;
101
        $mutee->registerIdentification(new Identification\PipedComposite($pipedComposite));
102
103
        return $mutee;
104
    }
105
106
    public function withIntegerId(int $integerId): self
107
    {
108
        $mutee = clone $this;
109
        $mutee->registerIdentification(new Identification\IntegerId($integerId));
110
111
        return $mutee;
112
    }
113
}
114