Completed
Pull Request — master (#135)
by Kristof
05:16
created

IriOfferIdentifier   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
c 2
b 1
f 0
lcom 1
cbo 1
dl 0
loc 100
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getIdFromIri() 0 11 1
A getIri() 0 4 1
A getId() 0 4 1
A getType() 0 4 1
A jsonSerialize() 0 7 1
A serialize() 0 4 1
A unserialize() 0 7 1
1
<?php
2
3
namespace CultuurNet\UDB3\Offer;
4
5
class IriOfferIdentifier implements OfferIdentifierInterface
6
{
7
    /**
8
     * @var string
9
     */
10
    private $iri;
11
12
    /**
13
     * @var string
14
     */
15
    private $id;
16
17
    /**
18
     * @var OfferType
19
     */
20
    private $type;
21
22
    /**
23
     * @param string $iri
24
     * @param OfferType $type
25
     */
26
    public function __construct(
27
        $iri,
28
        OfferType $type
29
    ) {
30
        $this->iri = $iri;
31
        $this->type = $type;
32
        $this->id = $this->getIdFromIri($iri);
33
    }
34
35
    /**
36
     * @param string $iri
37
     * @return string
38
     */
39
    private function getIdFromIri($iri)
40
    {
41
        // Remove any trailing slashes to be safe.
42
        $iri = rtrim($iri, '/');
43
44
        // Split the iri into multiple pieces.
45
        $exploded = explode('/', $iri);
46
47
        // The id is the last of all the separate pieces.
48
        return array_pop($exploded);
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getIri()
55
    {
56
        return $this->iri;
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getId()
63
    {
64
        return $this->id;
65
    }
66
67
    /**
68
     * @return OfferType
69
     */
70
    public function getType()
71
    {
72
        return $this->type;
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function jsonSerialize()
79
    {
80
        return [
81
            '@id' => $this->iri,
82
            '@type' => $this->type->toNative(),
83
        ];
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function serialize()
90
    {
91
        return json_encode($this->jsonSerialize());
92
    }
93
94
    /**
95
     * @param string $serialized
96
     */
97
    public function unserialize($serialized)
98
    {
99
        $data = json_decode($serialized, true);
100
        $this->iri = $data['@id'];
101
        $this->type = OfferType::fromNative($data['@type']);
102
        $this->id = $this->getIdFromIri($this->iri);
103
    }
104
}
105