Completed
Pull Request — master (#4)
by
unknown
14:30 queued 10:00
created

MediaTrait::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
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",cascade={"all"},
85
     *     orphanRemoval=true
86
     * )
87
     */
88
    protected $pageHasMedias;
89
90
    /**
91
     * @ORM\OneToMany(targetEntity="PiedWeb\CMSBundle\Entity\PageInterface", mappedBy="mainImage")
92
     */
93
    protected $mainImagePages;
94
95
    public function __toString()
96
    {
97
        return $this->name.' ';
98
    }
99
100
    public function setSlug($slug)
101
    {
102
        $this->setMedia($slug);
103
104
        return $this;
105
    }
106
107
    public function getSlug(): string
108
    {
109
        if ($this->slug) {
110
            return $this->slug;
111
        } elseif ($this->media) {
112
            $this->slug = preg_replace('/\\.[^.\\s]{3,4}$/', '', $this->media);
113
        } else {
114
            $slugify = new Slugify();
115
            $this->slug = $slugify->slugify($this->getName()); //Urlizer::urlize($this->getName());
116
        }
117
118
        return $this->slug;
119
    }
120
121
    public function setMediaFile(?File $media = null): void
122
    {
123
        $this->mediaFile = $media;
124
125
        if (null !== $media) { //normaly no more need with gedmo traits
126
            // It is required that at least one field changes if you are using doctrine
127
            // otherwise the event listeners won't be called and the file is lost
128
            $this->updatedAt = new \DateTimeImmutable();
129
        }
130
    }
131
132
    public function getMediaFile(): ?File
133
    {
134
        return $this->mediaFile;
135
    }
136
137
    public function getMedia(): ?string
138
    {
139
        return $this->media;
140
    }
141
142
    public function setMedia($media): self
143
    {
144
        $this->media = $media;
145
146
        return $this;
147
    }
148
149 3
    public function getName($locale = null): ?string
150
    {
151 3
        $names = $this->getNames(true);
152
153 3
        return $locale && isset($names[$locale]) ? $locale : $this->name;
154
    }
155
156 3
    public function getNames($yaml = false)
157
    {
158 3
        return true === $yaml && null !== $this->names ? Yaml::parse($this->names) : $this->names;
159
    }
160
161
    public function setNames(?string $names): self
162
    {
163
        $this->name = $names;
164
165
        return $this;
166
    }
167
168
    public function getNameByLocale($locale)
169
    {
170
        $names = $this->getNames(true);
171
172
        return $names[$locale] ?? $this->getName();
173
    }
174
175 3
    public function setName(?string $name): self
176
    {
177 3
        $this->name = $name;
178
179 3
        return $this;
180
    }
181
182
    public function getRelativeDir(): ?string
183
    {
184
        return $this->relativeDir;
185
    }
186
187
    public function setRelativeDir($relativeDir): self
188
    {
189
        $this->relativeDir = $relativeDir;
190
191
        return $this;
192
    }
193
194
    public function getMimeType(): ?string
195
    {
196
        return $this->mimeType;
197
    }
198
199
    public function setMimeType($mimeType): self
200
    {
201
        $this->mimeType = $mimeType;
202
203
        return $this;
204
    }
205
206
    public function getSize()
207
    {
208
        return $this->size;
209
    }
210
211
    public function setSize($size): self
212
    {
213
        $this->size = $size;
214
215
        return $this;
216
    }
217
218
    public function setDimensions($dimensions): self
219
    {
220
        if (isset($dimensions[0])) {
221
            $this->width = (int) $dimensions[0];
222
        }
223
224
        if (isset($dimensions[1])) {
225
            $this->height = (int) $dimensions[1];
226
        }
227
228
        return $this;
229
    }
230
231
    public function getDimensions(): array
232
    {
233
        return [$this->width, $this->height];
234
    }
235
236
    public function getRatio(): float
237
    {
238
        return $this->height / $this->width;
239
    }
240
241
    public function getWidth()
242
    {
243
        return $this->width;
244
    }
245
246
    public function getHeight()
247
    {
248
        return $this->height;
249
    }
250
251
    public function getMainColor(): ?string
252
    {
253
        return $this->mainColor;
254
    }
255
256
    public function setMainColor(?string $mainColor): self
257
    {
258
        $this->mainColor = $mainColor;
259
260
        return $this;
261
    }
262
263
    public function setPageHasMedias($pageHasMedias)
264
    {
265
        $this->pageHasMedias = new ArrayCollection();
266
        foreach ($pageHasMedias as $pageHasMedia) {
267
            $this->addPageHasMedia($pageHasMedia);
268
        }
269
    }
270
271
    public function getPageHasMedias()
272
    {
273
        return $this->pageHasMedias;
274
    }
275
276
    public function addPageHasMedia(PageHasMedia $pageHasMedia)
277
    {
278
        $pageHasMedia->setMedia($this);
279
        $this->pageHasMedias[] = $pageHasMedia;
280
    }
281
282
    public function removePageHasMedia(PageHasMedia $pageHasMedia)
283
    {
284
        $this->pageHasMedias->removeElement($pageHasMedia);
285
    }
286
287
    public function getMainImagePages()
288
    {
289
        return $this->mainImagePages;
290
    }
291
292
    public function getFullPath(): ?string
293
    {
294
        return null !== $this->media ? '/'.$this->getRelativeDir().'/'.$this->getMedia() : null;
295
    }
296
}
297