Passed
Push — master ( a93a71...e5d143 )
by Dev
13:49
created

MediaTrait::getFullPathWebP()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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