Entity::equals()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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