Passed
Push — master ( ea66cd...e08546 )
by Dev
12:54
created

MediaTrait::setSlug()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 11
ccs 0
cts 5
cp 0
crap 6
rs 10
1
<?php
2
3
namespace PiedWeb\CMSBundle\Entity;
4
5
use Cocur\Slugify\Slugify;
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Doctrine\ORM\Mapping as ORM;
8
use Gedmo\Mapping\Annotation as Gedmo;
9
use Gedmo\Sluggable\Util\Urlizer;
10
use Gedmo\Timestampable\Traits\TimestampableEntity;
11
use InvertColor\Color;
12
use Symfony\Component\HttpFoundation\File\File;
13
use Symfony\Component\Yaml\Yaml;
14
use Vich\UploaderBundle\Mapping\Annotation as Vich;
15
16
trait MediaTrait
17
{
18
    use TimestampableEntity;
19
20
    /**
21
     * @ORM\Column(type="string", length=50)
22
     */
23
    protected $mimeType;
24
25
    /**
26
     * @ORM\Column(type="string", length=255)
27
     */
28
    protected $relativeDir;
29
30
    /**
31
     * @ORM\Column(type="integer")
32
     */
33
    protected $size;
34
35
    /**
36
     * @ORM\Column(type="integer", nullable=true)
37
     */
38
    protected $height;
39
40
    /**
41
     * @ORM\Column(type="integer", nullable=true)
42
     */
43
    protected $width;
44
45
    /**
46
     * @ORM\Column(type="string", nullable=true)
47
     */
48
    protected $mainColor;
49
50
    /**
51
     * @ORM\Column(type="string", length=255)
52
     */
53
    protected $media;
54
55
    /**
56
     * NOTE: This is not a mapped field of entity metadata, just a simple property.
57
     *
58
     * @Vich\UploadableField(
59
     *     mapping="media_media",
60
     *     fileNameProperty="slug",
61
     *     mimeType="mimeType",
62
     *     size="size",
63
     *     dimensions="dimensions"
64
     * )
65
     *
66
     * @var File
67
     */
68
    protected $mediaFile;
69
70
    /**
71
     * @ORM\Column(type="string", length=100, unique=true)
72
     */
73
    protected $name;
74
75
    /**
76
     * @ORM\Column(type="text", nullable=true)
77
     */
78
    protected $names;
79
80
    protected $slug;
81
82
    /**
83
     * @ORM\OneToMany(
84
     *     targetEntity="PiedWeb\CMSBundle\Entity\PageHasMediaInterface",
85
     *     mappedBy="media",
86
     *     cascade={"all"},
87
     *     orphanRemoval=true
88
     * )
89
     */
90
    protected $pageHasMedias;
91
92
    /**
93
     * @ORM\OneToMany(
94
     *      targetEntity="PiedWeb\CMSBundle\Entity\PageInterface",
95
     *      mappedBy="mainImage"
96
     * )
97
     */
98
    protected $mainImagePages;
99
100
    public function __toString()
101
    {
102
        return $this->name.' ';
103
    }
104
105
    public function setSlug($slug, $force = false)
106
    {
107
        if (true === $force) {
108
            $this->slug = $slug;
109
110
            return $this;
111
        }
112
113
        $this->setMedia($slug);
114
115
        return $this;
116
    }
117
118
    public function getSlug(): string
119
    {
120
        if ($this->slug) {
121
            return $this->slug;
122
        }
123
124
        if ($this->media) {
125
            return $this->slug = preg_replace('/\\.[^.\\s]{3,4}$/', '', $this->media);
126
        }
127
128
        $slugify = new Slugify();
129
        $this->slug = $slugify->slugify($this->getName()); //Urlizer::urlize($this->getName());
130
131
        return $this->slug;
132
    }
133
134
    public function setMediaFile(?File $media = null): void
135
    {
136
        $this->mediaFile = $media;
137
138
        if (null !== $media) { //normaly no more need with gedmo traits
139
            // It is required that at least one field changes if you are using doctrine
140
            // otherwise the event listeners won't be called and the file is lost
141
            $this->updatedAt = new \DateTimeImmutable();
142
        }
143
    }
144
145
    public function getMediaFile(): ?File
146
    {
147
        return $this->mediaFile;
148
    }
149
150
    public function getMedia(): ?string
151
    {
152
        return $this->media;
153
    }
154
155
    public function setMedia($media): self
156
    {
157
        $this->media = $media;
158
159
        return $this;
160
    }
161 3
162
    public function getName($getLocalized = null, $onlyLocalized = false): ?string
163 3
    {
164
        $names = $this->getNames(true);
165 3
166
        return $getLocalized ?
167 3
            (isset($names[$getLocalized]) ? $names[$getLocalized] : ($onlyLocalized ? null : $this->name))
168
            : $this->name;
169
    }
170 3
171
    public function getNames($yaml = false)
172 3
    {
173
        return true === $yaml && null !== $this->names ? Yaml::parse($this->names) : $this->names;
174
    }
175
176
    public function setNames(?string $names): self
177
    {
178
        $this->name = $names;
179
180
        return $this;
181
    }
182
183
    public function getNameByLocale($locale)
184
    {
185
        $names = $this->getNames(true);
186
187
        return $names[$locale] ?? $this->getName();
188
    }
189 3
190
    public function setName(?string $name): self
191 3
    {
192
        $this->name = $name;
193 3
194
        return $this;
195
    }
196
197
    public function getRelativeDir(): ?string
198
    {
199
        return rtrim($this->relativeDir, '/');
200
    }
201
202
    public function setRelativeDir($relativeDir): self
203
    {
204
        $this->relativeDir = rtrim($relativeDir, '/');
205
206
        return $this;
207
    }
208
209
    public function getMimeType(): ?string
210
    {
211
        return $this->mimeType;
212
    }
213
214
    public function setMimeType($mimeType): self
215
    {
216
        $this->mimeType = $mimeType;
217
218
        return $this;
219
    }
220
221
    public function getSize()
222
    {
223
        return $this->size;
224
    }
225
226
    public function setSize($size): self
227
    {
228
        $this->size = $size;
229
230
        return $this;
231
    }
232
233
    public function setDimensions($dimensions): self
234
    {
235
        if (isset($dimensions[0])) {
236
            $this->width = (int) $dimensions[0];
237
        }
238
239
        if (isset($dimensions[1])) {
240
            $this->height = (int) $dimensions[1];
241
        }
242
243
        return $this;
244
    }
245
246
    public function getDimensions(): array
247
    {
248
        return [$this->width, $this->height];
249
    }
250
251
    public function getRatio(): float
252
    {
253
        return $this->height / $this->width;
254
    }
255
256
    public function getWidth()
257
    {
258
        return $this->width;
259
    }
260
261
    public function getHeight()
262
    {
263
        return $this->height;
264
    }
265
266
    public function getMainColor(): ?string
267
    {
268
        return $this->mainColor;
269
    }
270
271
    public function getMainColorOpposite()
272
    {
273
        if (null === $this->getMainColor()) {
274
            return;
275
        }
276
277
        return Color::fromHex($this->getMainColor())->invert(true);
278
    }
279
280
    public function setMainColor(?string $mainColor): self
281
    {
282
        $this->mainColor = $mainColor;
283
284
        return $this;
285
    }
286
287
    public function setPageHasMedias($pageHasMedias)
288
    {
289
        $this->pageHasMedias = new ArrayCollection();
290
        foreach ($pageHasMedias as $pageHasMedia) {
291
            $this->addPageHasMedia($pageHasMedia);
292
        }
293
    }
294
295
    public function getPageHasMedias()
296
    {
297
        return $this->pageHasMedias;
298
    }
299
300
    public function addPageHasMedia(PageHasMedia $pageHasMedia)
301
    {
302
        $pageHasMedia->setMedia($this);
303
        $this->pageHasMedias[] = $pageHasMedia;
304
    }
305
306
    public function removePageHasMedia(PageHasMedia $pageHasMedia)
307
    {
308
        $this->pageHasMedias->removeElement($pageHasMedia);
309
    }
310
311
    public function getMainImagePages()
312
    {
313
        return $this->mainImagePages;
314
    }
315
316
    /**
317
     * @ORM\PreRemove
318
     */
319
    public function removeMainImageFromPages()
320
    {
321
        foreach ($this->mainImagePages as $page) {
322
            $page->setMainImage(null);
323
        }
324
    }
325
326
    public function getPath(): ?string
327
    {
328
        return $this->getFullPath();
329
    }
330
331
    public function getFullPath(): ?string
332
    {
333
        return null !== $this->media
334
            ? '/'.$this->getRelativeDir().($this->getMedia() ? '/'.$this->getMedia() : '')
335
            : null;
336
    }
337
338
    public function getFullPathWebP(): ?string
339
    {
340
        return null !== $this->media
341
            ? '/'.$this->getRelativeDir().($this->getSlug() ? '/'.$this->getSlug().'.webp' : '')
342
            : null;
343
    }
344
}
345