Completed
Pull Request — master (#105)
by Kristof
13:40 queued 05:31
created

MediaObject   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

13 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 1
A getAggregateRootId() 0 4 1
A applyMediaObjectCreated() 0 7 1
A getUrl() 0 4 1
A getThumbnailUrl() 0 4 1
A getDescription() 0 4 1
A getCopyrightHolder() 0 4 1
A getFileId() 0 4 1
A getMimeType() 0 4 1
A deserialize() 0 4 1
A serialize() 0 11 1
A toJsonLd() 0 15 1
A setUrl() 0 5 1
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
    public static function create(UUID $fileId, MIMEType $fileType, String $description, String $copyrightHolder)
63
    {
64
        $mediaObject = new self();
65
        $mediaObject->apply(new MediaObjectCreated($fileId, $fileType, $description, $copyrightHolder));
66
67
        return $mediaObject;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getAggregateRootId()
74
    {
75
        return $this->fileId;
76
    }
77
78
    protected function applyMediaObjectCreated(MediaObjectCreated $mediaObjectCreated)
79
    {
80
        $this->fileId = $mediaObjectCreated->getFileId();
81
        $this->mimeType = $mediaObjectCreated->getMimeType();
82
        $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...
83
        $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...
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getUrl()
90
    {
91
        return $this->url;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getThumbnailUrl()
98
    {
99
        return $this->thumbnailUrl;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getDescription()
106
    {
107
        return $this->description;
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function getCopyrightHolder()
114
    {
115
        return $this->copyrightHolder;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getFileId()
122
    {
123
        return $this->fileId;
124
    }
125
126
    /**
127
     * @return MIMEType
128
     */
129
    public function getMimeType()
130
    {
131
        return $this->mimeType;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public static function deserialize(array $data)
138
    {
139
        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...
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function serialize()
146
    {
147
        return [
148
            'mime_type' => $this->mimeType,
149
            'url' => (string) $this->url,
150
            'thumbnail_url' => (string) $this->thumbnailUrl,
151
            'description' => (string) $this->description,
152
            'copyright_holder' => (string) $this->copyrightHolder,
153
            'file_id' => (string) $this->fileId,
154
        ];
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     * TODO: This should probably be moved to a projector
160
     */
161
    public function toJsonLd()
162
    {
163
        $jsonLd = [
164
            // TODO: use an iri generator to generate a proper id
165
            '@id' => (string) $this->getFileId(),
166
            // TODO: base type off of MIME
167
            '@type' => 'schema:MediaObject',
168
            'contentUrl' => (string) $this->url,
169
            'thumbnailUrl' => (string) $this->thumbnailUrl,
170
            'description' => (string) $this->description,
171
            'copyrightHolder' => (string) $this->copyrightHolder,
172
        ];
173
174
        return $jsonLd;
175
    }
176
177
    public function setUrl(Url $url)
178
    {
179
        $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...
180
        $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...
181
    }
182
}
183