Completed
Push — master ( f93802...a3beff )
by Paweł
34:14
created

ArticleMedia::addRendition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Content Bundle.
7
 *
8
 * Copyright 2016 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\Model;
18
19
use SWP\Component\Bridge\Model\ItemInterface;
20
use SWP\Component\Common\Model\TimestampableTrait;
21
use Doctrine\Common\Collections\ArrayCollection;
22
23
/**
24
 * ArticleMedia represents media which belongs to Article.
25
 */
26
class ArticleMedia implements ArticleMediaInterface
27
{
28
    use TimestampableTrait;
29
30
    /**
31
     * @var int
32
     */
33
    protected $id;
34
35
    /**
36
     * @var string
37
     */
38
    protected $key;
39
40
    /**
41
     * @var FileInterface
42
     */
43
    protected $file;
44
45
    /**
46
     * @var ImageInterface
47
     */
48
    protected $image;
49
50
    /**
51
     * @var ArticleInterface
52
     */
53
    protected $article;
54
55
    /**
56
     * @var string
57
     */
58
    protected $description;
59
60
    /**
61
     * @var string
62
     */
63
    protected $located;
64
65
    /**
66
     * @var string
67
     */
68
    protected $byLine;
69
70
    /**
71
     * @var string
72
     */
73
    protected $body;
74
75
    /**
76
     * @var string
77
     */
78
    protected $mimetype;
79
80
    /**
81
     * @var string
82
     */
83
    protected $usageTerms;
84
85
    /**
86
     * @var ArrayCollection
87
     */
88
    protected $renditions;
89
90
    /**
91
     * ArticleMedia constructor.
92
     */
93
    public function __construct()
94
    {
95
        $this->renditions = new ArrayCollection();
96
        $this->setCreatedAt(new \DateTime());
97
    }
98
99
    /**
100
     * @return ArrayCollection
101
     */
102
    public function getRenditions()
103
    {
104
        return $this->renditions;
105
    }
106
107
    /**
108
     * @param ImageRendition $rendition
109
     */
110
    public function addRendition(ImageRendition $rendition)
111
    {
112
        $this->renditions->add($rendition);
113
    }
114
115
    /**
116
     * @param ArrayCollection $renditions
117
     */
118
    public function setRenditions($renditions)
119
    {
120
        $this->renditions = $renditions;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function getId()
127
    {
128
        return $this->id;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function getFile()
135
    {
136
        return $this->file;
137
    }
138
139
    /**
140
     * @param FileInterface $file
141
     *
142
     * @return ArticleMedia
143
     */
144
    public function setFile($file)
145
    {
146
        $this->file = $file;
147
148
        return $this;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function getImage()
155
    {
156
        return $this->image;
157
    }
158
159
    /**
160
     * @param ImageInterface $image
161
     *
162
     * @return ArticleMedia
163
     */
164
    public function setImage($image)
165
    {
166
        $this->image = $image;
167
168
        return $this;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function getArticle()
175
    {
176
        return $this->article;
177
    }
178
179
    /**
180
     * @param ArticleInterface $article
181
     *
182
     * @return ArticleMedia
183
     */
184
    public function setArticle(ArticleInterface $article)
185
    {
186
        $this->article = $article;
187
188
        return $this;
189
    }
190
191
    public function getAssetId()
192
    {
193
        if ($this->getImage() instanceof Image) {
194
            return $this->getImage()->getAssetId();
195
        } elseif ($this->getFile() instanceof File) {
196
            return $this->getFile()->getAssetId();
197
        }
198
199
        return;
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function getDescription()
206
    {
207
        return $this->description;
208
    }
209
210
    /**
211
     * @param string $description
212
     *
213
     * @return ArticleMedia
214
     */
215
    public function setDescription($description)
216
    {
217
        $this->description = $description;
218
219
        return $this;
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function getLocated()
226
    {
227
        return $this->located;
228
    }
229
230
    /**
231
     * @param string $located
232
     *
233
     * @return ArticleMedia
234
     */
235
    public function setLocated($located)
236
    {
237
        $this->located = $located;
238
239
        return $this;
240
    }
241
242
    /**
243
     * {@inheritdoc}
244
     */
245
    public function getByLine()
246
    {
247
        return $this->byLine;
248
    }
249
250
    /**
251
     * @param string $byLine
252
     *
253
     * @return ArticleMedia
254
     */
255
    public function setByLine($byLine)
256
    {
257
        $this->byLine = $byLine;
258
259
        return $this;
260
    }
261
262
    /**
263
     * {@inheritdoc}
264
     */
265
    public function getBody()
266
    {
267
        return $this->body;
268
    }
269
270
    /**
271
     * @param string $body
272
     *
273
     * @return ArticleMedia
274
     */
275
    public function setBody($body)
276
    {
277
        $this->body = $body;
278
279
        return $this;
280
    }
281
282
    /**
283
     * @return string
284
     */
285
    public function getMimetype()
286
    {
287
        return $this->mimetype;
288
    }
289
290
    /**
291
     * @param string $mimetype
292
     *
293
     * @return ArticleMedia
294
     */
295
    public function setMimetype($mimetype)
296
    {
297
        $this->mimetype = $mimetype;
298
299
        return $this;
300
    }
301
302
    /**
303
     * @return mixed
304
     */
305
    public function getUsageTerms()
306
    {
307
        return $this->usageTerms;
308
    }
309
310
    /**
311
     * @param mixed $usageTerms
312
     *
313
     * @return ArticleMedia
314
     */
315
    public function setUsageTerms($usageTerms)
316
    {
317
        $this->usageTerms = $usageTerms;
318
319
        return $this;
320
    }
321
322
    /**
323
     * @return string
324
     */
325
    public function getKey(): string
326
    {
327
        return $this->key;
328
    }
329
330
    /**
331
     * @param string $key
332
     */
333
    public function setKey(string $key)
334
    {
335
        $this->key = $key;
336
    }
337
338
    /**
339
     * @param ItemInterface $item
340
     */
341
    public function setFromItem(ItemInterface $item)
342
    {
343
        $this->setBody($item->getBody() ?: $item->getBodyText());
344
        $this->setByLine($item->getByLine());
345
        $this->setLocated($item->getLocated());
346
        $this->setDescription($item->getDescription());
347
        $this->setUsageTerms($item->getUsageTerms());
348
    }
349
350
    /**
351
     * {@inheritdoc}
352
     */
353
    public static function handleMediaId($mediaId)
354
    {
355
        $mediaId = preg_replace('/\\.[^.\\s]{3,4}$/', '', $mediaId);
356
        $mediaIdElements = explode('/', $mediaId);
357
        if (count($mediaIdElements) == 2) {
358
            return $mediaIdElements[1];
359
        }
360
361
        return $mediaId;
362
    }
363
}
364