Completed
Pull Request — master (#105)
by Kristof
18:24 queued 13:13
created

MediaObject::getAggregateRootId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace CultuurNet\UDB3\Media;
4
5
use Broadway\EventSourcing\EventSourcedAggregateRoot;
6
use Broadway\Serializer\SerializableInterface;
7
use CultuurNet\UDB3\JsonLdSerializableInterface;
8
use CultuurNet\UDB3\Media\Events\MediaObjectCreated;
9
use CultuurNet\UDB3\Media\Properties\MIMEType;
10
use ValueObjects\Identity\UUID;
11
use ValueObjects\String\String;
12
use ValueObjects\Web\Url;
13
14
/**
15
 * MediaObjects for UDB3.
16
 */
17
class MediaObject extends EventSourcedAggregateRoot implements SerializableInterface, JsonLdSerializableInterface
18
{
19
20
    /**
21
     * Mime type of the media object.
22
     *
23
     * @var MIMEType
24
     */
25
    protected $mimeType;
26
27
    /**
28
     * File id.
29
     *
30
     * @var UUID
31
     */
32
    protected $fileId;
33
34
    /**
35
     * Url to the media object.
36
     *
37
     * @var string
38
     */
39
    protected $url;
40
41
    /**
42
     * Url to the thumbnail for the media object.
43
     *
44
     * @var string
45
     */
46
    protected $thumbnailUrl;
47
48
    /**
49
     * Description of the mediaobject.
50
     *
51
     * @var string
52
     */
53
    protected $description;
54
55
    /**
56
     * Copyright info.
57
     *
58
     * @var string
59
     */
60
    protected $copyrightHolder;
61
62
    /**
63
     * @param UUID $fileId
64
     * @param MIMEType $fileType
65
     * @param String $description
66
     * @param String $copyrightHolder
67
     * @return MediaObject
68
     */
69
    public static function create(UUID $fileId, MIMEType $fileType, String $description, String $copyrightHolder)
70
    {
71
        $mediaObject = new self();
72
        $mediaObject->apply(new MediaObjectCreated($fileId, $fileType, $description, $copyrightHolder));
73
74
        return $mediaObject;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getAggregateRootId()
81
    {
82
        return $this->fileId;
83
    }
84
85
    protected function applyMediaObjectCreated(MediaObjectCreated $mediaObjectCreated)
86
    {
87
        $this->fileId = $mediaObjectCreated->getFileId();
88
        $this->mimeType = $mediaObjectCreated->getMimeType();
89
        $this->description = $mediaObjectCreated->getDescription();
0 ignored issues
show
Documentation Bug introduced by
It seems like $mediaObjectCreated->getDescription() of type object<ValueObjects\String\String> is incompatible with the declared type string of property $description.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
90
        $this->copyrightHolder = $mediaObjectCreated->getCopyrightHolder();
0 ignored issues
show
Documentation Bug introduced by
It seems like $mediaObjectCreated->getCopyrightHolder() of type object<ValueObjects\String\String> is incompatible with the declared type string of property $copyrightHolder.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getUrl()
97
    {
98
        return $this->url;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getThumbnailUrl()
105
    {
106
        return $this->thumbnailUrl;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getDescription()
113
    {
114
        return $this->description;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getCopyrightHolder()
121
    {
122
        return $this->copyrightHolder;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getFileId()
129
    {
130
        return $this->fileId;
131
    }
132
133
    /**
134
     * @return MIMEType
135
     */
136
    public function getMimeType()
137
    {
138
        return $this->mimeType;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public static function deserialize(array $data)
145
    {
146
        return new static($data['url'], $data['mime_type'], $data['thumbnail_url'], $data['description'], $data['copyright_holder'], $data['file_id'], $type);
0 ignored issues
show
Bug introduced by
The variable $type does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Unused Code introduced by
The call to MediaObject::__construct() has too many arguments starting with $data['url'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function serialize()
153
    {
154
        return [
155
            'mime_type' => $this->mimeType,
156
            'url' => (string) $this->url,
157
            'thumbnail_url' => (string) $this->thumbnailUrl,
158
            'description' => (string) $this->description,
159
            'copyright_holder' => (string) $this->copyrightHolder,
160
            'file_id' => (string) $this->fileId,
161
        ];
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     * TODO: This should probably be moved to a projector
167
     */
168
    public function toJsonLd()
169
    {
170
        $jsonLd = [
171
            // TODO: use an iri generator to generate a proper id
172
            '@id' => (string) $this->getFileId(),
173
            // TODO: base type off of MIME
174
            '@type' => 'schema:MediaObject',
175
            'contentUrl' => (string) $this->url,
176
            'thumbnailUrl' => (string) $this->thumbnailUrl,
177
            'description' => (string) $this->description,
178
            'copyrightHolder' => (string) $this->copyrightHolder,
179
        ];
180
181
        return $jsonLd;
182
    }
183
184
    public function setUrl(Url $url)
185
    {
186
        $this->url = $url;
0 ignored issues
show
Documentation Bug introduced by
It seems like $url of type object<ValueObjects\Web\Url> is incompatible with the declared type string of property $url.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
187
        $this->thumbnailUrl = $url;
0 ignored issues
show
Documentation Bug introduced by
It seems like $url of type object<ValueObjects\Web\Url> is incompatible with the declared type string of property $thumbnailUrl.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
188
    }
189
}
190