Completed
Push — develop ( dae12f...f18ec7 )
by Daan van
02:40
created

Entity::equals()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace OpenConext\Value\Saml;
4
5
use OpenConext\Value\Exception\InvalidArgumentException;
6
7
final class Entity
8
{
9
    /**
10
     * @var EntityId
11
     */
12
    private $entityId;
13
14
    /**
15
     * @var EntityType
16
     */
17
    private $entityType;
18
19
    /**
20
     * @param array $descriptor array('entity-id', 'sp or idp')
21
     * @return Entity
22
     */
23
    public static function fromDescriptor(array $descriptor)
24
    {
25
        if (count($descriptor) !== 2) {
26
            throw new InvalidArgumentException(
27
                'EntityDescriptor must be an array with two elements (both a string), the first must be the EntityId,'
28
                . ' the second the EntityType'
29
            );
30
        }
31
32
        switch ($descriptor[1]) {
33
            case 'sp':
34
                return new Entity(new EntityId($descriptor[0]), EntityType::SP());
35
            case 'idp':
36
                return new Entity(new EntityId($descriptor[0]), EntityType::IdP());
37
            default:
38
                throw new InvalidArgumentException('Entity descriptor type is neither "sp" nor "idp"');
39
        }
40
    }
41
42
    public function __construct(EntityId $entityId, EntityType $entityType)
43
    {
44
        $this->entityId   = $entityId;
45
        $this->entityType = $entityType;
46
    }
47
48
    /**
49
     * @param EntityId $entityId
50
     * @return bool
51
     */
52
    public function hasEntityId(EntityId $entityId)
53
    {
54
        return $this->entityId->equals($entityId);
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    public function isServiceProvider()
61
    {
62
        return $this->entityType->isServiceProvider();
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function isIdentityProvider()
69
    {
70
        return $this->entityType->isIdentityProvider();
71
    }
72
73
    /**
74
     * @return EntityId
75
     */
76
    public function getEntityId()
77
    {
78
        return $this->entityId;
79
    }
80
81
    /**
82
     * @return EntityType
83
     */
84
    public function getEntityType()
85
    {
86
        return $this->entityType;
87
    }
88
89
    /**
90
     * @param Entity $other
91
     * @return bool
92
     */
93
    public function equals(Entity $other)
94
    {
95
        return $this->entityId->equals($other->entityId) && $this->entityType->equals($other->entityType);
96
    }
97
98
    public function __toString()
99
    {
100
        return $this->entityId . ' (' . $this->entityType . ')';
101
    }
102
}
103