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

IriOfferIdentifierJSONDeserializer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A deserialize() 0 20 4
1
<?php
2
3
namespace CultuurNet\UDB3\Offer;
4
5
use CultuurNet\Deserializer\DeserializerInterface;
6
use CultuurNet\Deserializer\MissingValueException;
7
use CultuurNet\Deserializer\NotWellFormedException;
8
use ValueObjects\String\String;
9
10
class IriOfferIdentifierJSONDeserializer implements DeserializerInterface
11
{
12
    /**
13
     * @param String $data
14
     * @return IriOfferIdentifier
15
     */
16
    public function deserialize(String $data)
17
    {
18
        $data = json_decode($data->toNative(), true);
19
20
        if (null === $data) {
21
            throw new NotWellFormedException('Invalid JSON');
22
        }
23
24
        if (!isset($data['@id'])) {
25
            throw new MissingValueException('Missing property "@id".');
26
        }
27
        if (!isset($data['@type'])) {
28
            throw new MissingValueException('Missing property "@type".');
29
        }
30
31
        return new IriOfferIdentifier(
32
            $data['@id'],
33
            OfferType::fromNative($data['@type'])
34
        );
35
    }
36
}
37