Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

ArticleMedia::setFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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
22
/**
23
 * ArticleMedia represents media which belongs to Article.
24
 */
25
abstract class ArticleMedia implements ArticleMediaInterface
26
{
27
    use TimestampableTrait;
28
29
    /**
30
     * @var int
31
     */
32
    protected $id;
33
34
    /**
35
     * @var string
36
     */
37
    protected $key;
38
39
    /**
40
     * @var FileInterface
41
     */
42
    protected $file;
43
44
    /**
45
     * @var ImageInterface
46
     */
47
    protected $image;
48
49
    /**
50
     * @var ArticleInterface
51
     */
52
    protected $article;
53
54
    /**
55
     * @var string
56
     */
57
    protected $description;
58
59
    /**
60
     * @var string
61
     */
62
    protected $located;
63
64
    /**
65
     * @var string
66
     */
67
    protected $byLine;
68
69
    /**
70
     * @var string
71
     */
72
    protected $body;
73
74
    /**
75
     * @var string
76
     */
77
    protected $mimetype;
78
79
    /**
80
     * @var string
81
     */
82
    protected $usageTerms;
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 2
    public function getId()
88
    {
89 2
        return $this->id;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getFile()
96
    {
97
        return $this->file;
98
    }
99
100
    /**
101
     * @param FileInterface $file
102
     *
103
     * @return ArticleMedia
104
     */
105
    public function setFile($file)
106
    {
107
        $this->file = $file;
108
109
        return $this;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115 1
    public function getImage()
116
    {
117 1
        return $this->image;
118
    }
119
120
    /**
121
     * @param ImageInterface $image
122
     *
123
     * @return ArticleMedia
124
     */
125 6
    public function setImage($image)
126
    {
127 6
        $this->image = $image;
128
129 6
        return $this;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function getArticle()
136
    {
137
        return $this->article;
138
    }
139
140
    /**
141
     * @param ArticleInterface $article
142
     *
143
     * @return ArticleMedia
144
     */
145 6
    public function setArticle(ArticleInterface $article)
146
    {
147 6
        $this->article = $article;
148
149 6
        return $this;
150
    }
151
152
    public function getAssetId()
153
    {
154
        if ($this->getImage() instanceof Image) {
155
            return $this->getImage()->getAssetId();
156
        } elseif ($this->getFile() instanceof File) {
157
            return $this->getFile()->getAssetId();
158
        }
159
160
        return;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 2
    public function getDescription()
167
    {
168 2
        return $this->description;
169
    }
170
171
    /**
172
     * @param string $description
173
     *
174
     * @return ArticleMedia
175
     */
176 6
    public function setDescription($description)
177
    {
178 6
        $this->description = $description;
179
180 6
        return $this;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186 2
    public function getLocated()
187
    {
188 2
        return $this->located;
189
    }
190
191
    /**
192
     * @param string $located
193
     *
194
     * @return ArticleMedia
195
     */
196 6
    public function setLocated($located)
197
    {
198 6
        $this->located = $located;
199
200 6
        return $this;
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206 2
    public function getByLine()
207
    {
208 2
        return $this->byLine;
209
    }
210
211
    /**
212
     * @param string $byLine
213
     *
214
     * @return ArticleMedia
215
     */
216 6
    public function setByLine($byLine)
217
    {
218 6
        $this->byLine = $byLine;
219
220 6
        return $this;
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226 2
    public function getBody()
227
    {
228 2
        return $this->body;
229
    }
230
231
    /**
232
     * @param string $body
233
     *
234
     * @return ArticleMedia
235
     */
236 6
    public function setBody($body)
237
    {
238 6
        $this->body = $body;
239
240 6
        return $this;
241
    }
242
243
    /**
244
     * @return string
245
     */
246 2
    public function getMimetype()
247
    {
248 2
        return $this->mimetype;
249
    }
250
251
    /**
252
     * @param string $mimetype
253
     *
254
     * @return ArticleMedia
255
     */
256 6
    public function setMimetype($mimetype)
257
    {
258 6
        $this->mimetype = $mimetype;
259
260 6
        return $this;
261
    }
262
263
    /**
264
     * @return mixed
265
     */
266 2
    public function getUsageTerms()
267
    {
268 2
        return $this->usageTerms;
269
    }
270
271
    /**
272
     * @param mixed $usageTerms
273
     *
274
     * @return ArticleMedia
275
     */
276 6
    public function setUsageTerms($usageTerms)
277
    {
278 6
        $this->usageTerms = $usageTerms;
279
280 6
        return $this;
281
    }
282
283
    /**
284
     * @return string
285
     */
286 3
    public function getKey(): string
287
    {
288 3
        return $this->key;
289
    }
290
291
    /**
292
     * @param string $key
293
     */
294 6
    public function setKey(string $key)
295
    {
296 6
        $this->key = $key;
297 6
    }
298
299
    /**
300
     * @param ItemInterface $item
301
     */
302 4
    public function setFromItem(ItemInterface $item)
303
    {
304 4
        $this->setBody($item->getBody() ?: $item->getBodyText());
305 4
        $this->setByLine($item->getByLine());
306 4
        $this->setLocated($item->getLocated());
307 4
        $this->setDescription($item->getDescription());
308 4
        $this->setUsageTerms($item->getUsageTerms());
309 4
    }
310
311
    /**
312
     * {@inheritdoc}
313
     */
314 9
    public static function handleMediaId($mediaId)
315
    {
316 9
        $mediaId = preg_replace('/\\.[^.\\s]{3,4}$/', '', $mediaId);
317 9
        $mediaIdElements = explode('/', $mediaId);
318 9
        if (count($mediaIdElements) == 2) {
319 2
            return $mediaIdElements[1];
320
        }
321
322 9
        return $mediaId;
323
    }
324
}
325