Completed
Push — master ( 58748f...bc04d6 )
by Dev
04:22
created

MediaTrait::getMainImagePages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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($getLocalized = null, $onlyLocalized = false): ?string
150
    {
151 3
        $names = $this->getNames(true);
152
153 3
        return $getLocalized ?
154
            (isset($names[$locale]) ? $names[$locale] : ($onlyLocalized ? null : $this->name))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $locale seems to be never defined.
Loading history...
155 3
            : $this->name;
156
    }
157
158 3
    public function getNames($yaml = false)
159
    {
160 3
        return true === $yaml && null !== $this->names ? Yaml::parse($this->names) : $this->names;
161
    }
162
163
    public function setNames(?string $names): self
164
    {
165
        $this->name = $names;
166
167
        return $this;
168
    }
169
170
    public function getNameByLocale($locale)
171
    {
172
        $names = $this->getNames(true);
173
174
        return $names[$locale] ?? $this->getName();
175
    }
176
177 3
    public function setName(?string $name): self
178
    {
179 3
        $this->name = $name;
180
181 3
        return $this;
182
    }
183
184
    public function getRelativeDir(): ?string
185
    {
186
        return $this->relativeDir;
187
    }
188
189
    public function setRelativeDir($relativeDir): self
190
    {
191
        $this->relativeDir = $relativeDir;
192
193
        return $this;
194
    }
195
196
    public function getMimeType(): ?string
197
    {
198
        return $this->mimeType;
199
    }
200
201
    public function setMimeType($mimeType): self
202
    {
203
        $this->mimeType = $mimeType;
204
205
        return $this;
206
    }
207
208
    public function getSize()
209
    {
210
        return $this->size;
211
    }
212
213
    public function setSize($size): self
214
    {
215
        $this->size = $size;
216
217
        return $this;
218
    }
219
220
    public function setDimensions($dimensions): self
221
    {
222
        if (isset($dimensions[0])) {
223
            $this->width = (int) $dimensions[0];
224
        }
225
226
        if (isset($dimensions[1])) {
227
            $this->height = (int) $dimensions[1];
228
        }
229
230
        return $this;
231
    }
232
233
    public function getDimensions(): array
234
    {
235
        return [$this->width, $this->height];
236
    }
237
238
    public function getRatio(): float
239
    {
240
        return $this->height / $this->width;
241
    }
242
243
    public function getWidth()
244
    {
245
        return $this->width;
246
    }
247
248
    public function getHeight()
249
    {
250
        return $this->height;
251
    }
252
253
    public function getMainColor(): ?string
254
    {
255
        return $this->mainColor;
256
    }
257
258
    public function setMainColor(?string $mainColor): self
259
    {
260
        $this->mainColor = $mainColor;
261
262
        return $this;
263
    }
264
265
    public function setPageHasMedias($pageHasMedias)
266
    {
267
        $this->pageHasMedias = new ArrayCollection();
268
        foreach ($pageHasMedias as $pageHasMedia) {
269
            $this->addPageHasMedia($pageHasMedia);
270
        }
271
    }
272
273
    public function getPageHasMedias()
274
    {
275
        return $this->pageHasMedias;
276
    }
277
278
    public function addPageHasMedia(PageHasMedia $pageHasMedia)
279
    {
280
        $pageHasMedia->setMedia($this);
281
        $this->pageHasMedias[] = $pageHasMedia;
282
    }
283
284
    public function removePageHasMedia(PageHasMedia $pageHasMedia)
285
    {
286
        $this->pageHasMedias->removeElement($pageHasMedia);
287
    }
288
289
    public function getMainImagePages()
290
    {
291
        return $this->mainImagePages;
292
    }
293
294
    public function getFullPath(): ?string
295
    {
296
        return null !== $this->media ? '/'.$this->getRelativeDir().'/'.$this->getMedia() : null;
297
        // todo : check if / is needed
298
    }
299
}
300