Completed
Pull Request — master (#354)
by
unknown
04:56
created

MediaObjectSerializer::serialize()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 3
nop 3
1
<?php
2
3
namespace CultuurNet\UDB3\Media\Serialization;
4
5
use CultuurNet\UDB3\Iri\IriGeneratorInterface;
6
use CultuurNet\UDB3\Media\Image;
7
use CultuurNet\UDB3\Media\MediaObject;
8
use CultuurNet\UDB3\Media\Properties\MIMEType;
9
use Symfony\Component\Serializer\Exception\UnsupportedException;
10
use Symfony\Component\Serializer\SerializerInterface;
11
12
class MediaObjectSerializer implements SerializerInterface
13
{
14
    /**
15
     * @var IriGeneratorInterface
16
     */
17
    protected $iriGenerator;
18
19
    /**
20
     * MediaObjectSerializer constructor.
21
     * @param IriGeneratorInterface $iriGenerator
22
     */
23
    public function __construct(
24
        IriGeneratorInterface $iriGenerator
25
    ) {
26
        $this->iriGenerator = $iriGenerator;
27
    }
28
29
    /**
30
     * @param MediaObject|Image $mediaObject
31
     * @param string $format
32
     * @param array $context
33
     * @return array
34
     */
35
    public function serialize($mediaObject, $format, array $context = array())
36
    {
37
        if (!isset($format) || $format !== 'json-ld') {
38
            throw new UnsupportedException('Unsupported format, only json-ld is available.');
39
        };
40
41
        if ($mediaObject instanceof Image) {
42
            // Some Image objects have the 'application/octet-stream' mime-type, so we hardcode the @type to
43
            // 'schema:ImageObject' to make sure an Image does not get the @type 'schema:mediaObject'.
44
            $type = 'schema:ImageObject';
45
        } else {
46
            $type = $this->serializeMimeType($mediaObject->getMimeType());
47
        }
48
49
        $normalizedData = [
50
            '@id' => $this->iriGenerator->iri($mediaObject->getMediaObjectId()),
51
            '@type' => $type,
52
            'contentUrl' => (string) $mediaObject->getSourceLocation(),
53
            'thumbnailUrl' => (string) $mediaObject->getSourceLocation(),
54
            'description' => (string) $mediaObject->getDescription(),
55
            'copyrightHolder' => (string) $mediaObject->getCopyrightHolder(),
56
            'inLanguage' => (string) $mediaObject->getLanguage(),
57
        ];
58
59
        return $normalizedData;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $normalizedData; (array<string,string>) is incompatible with the return type declared by the interface Symfony\Component\Serial...zerInterface::serialize of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
60
    }
61
62
    public function serializeMimeType(MIMEType $mimeType)
63
    {
64
        $typeParts = explode('/', (string) $mimeType);
65
        $type = array_shift($typeParts);
66
67
        if ($type === 'image') {
68
            return 'schema:ImageObject';
69
        }
70
71
        if ((string) $mimeType === 'application/octet-stream') {
72
            return 'schema:mediaObject';
73
        }
74
75
        throw new UnsupportedException('Unsupported MIME-type "'. $mimeType .'"');
76
    }
77
78
    public function deserialize($data, $type, $format, array $context = array())
79
    {
80
        throw new \Exception('Deserialization currently not supported.');
81
    }
82
}
83