Passed
Push — master ( f7c6bc...694fd3 )
by Dev
10:07
created

MediaTrait::getMediaFile()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
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 Vich\UploaderBundle\Mapping\Annotation as Vich;
13
14
trait MediaTrait
15
{
16
    use TimestampableEntity;
17
18
    /**
19
     * @ORM\Column(type="string", length=50)
20
     */
21
    protected $mimeType;
22
23
    /**
24
     * @ORM\Column(type="string", length=255)
25
     */
26
    protected $relativeDir;
27
28
    /**
29
     * @ORM\Column(type="integer")
30
     */
31
    protected $size;
32
33
    /**
34
     * @ORM\Column(type="integer", nullable=true)
35
     */
36
    protected $height;
37
38
    /**
39
     * @ORM\Column(type="integer", nullable=true)
40
     */
41
    protected $width;
42
43
    /**
44
     * @ORM\Column(type="string", nullable=true)
45
     */
46
    protected $mainColor;
47
48
    /**
49
     * @ORM\Column(type="string", length=255)
50
     */
51
    protected $media;
52
53
    /**
54
     * NOTE: This is not a mapped field of entity metadata, just a simple property.
55
     *
56
     * @Vich\UploadableField(
57
     *     mapping="media_media",
58
     *     fileNameProperty="slug",
59
     *     mimeType="mimeType",
60
     *     size="size",
61
     *     dimensions="dimensions"
62
     * )
63
     *
64
     * @var File
65
     */
66
    protected $mediaFile;
67
68
    /**
69
     * @Gedmo\Translatable
70
     * @ORM\Column(type="string", length=100, unique=true)
71
     */
72
    protected $name;
73
74
    protected $slug;
75
76
    /**
77
     * @ORM\OneToMany(
78
     *     targetEntity="PiedWeb\CMSBundle\Entity\PageHasMediaInterface",
79
     *     mappedBy="media",cascade={"all"},
80
     *     orphanRemoval=true
81
     * )
82
     */
83
    protected $pageHasMedias;
84
85
    public function __toString()
86
    {
87
        return $this->name.' ';
88
    }
89
90
    public function setSlug($slug)
91
    {
92
        $this->setMedia($slug);
93
94
        return $this;
95
    }
96
97
    public function getSlug(): string
98
    {
99
        if ($this->slug) {
100
            return $this->slug;
101
        } elseif ($this->media) {
102
            $this->slug = preg_replace('/\\.[^.\\s]{3,4}$/', '', $this->media);
103
        } else {
104
            $slugify = new Slugify();
105
            $this->slug = $slugify->slugify($this->getName()); //Urlizer::urlize($this->getName());
106
        }
107
108
        return $this->slug;
109
    }
110
111
    public function setMediaFile(?File $media = null): void
112
    {
113
        $this->mediaFile = $media;
114
115
        if (null !== $media) { //normaly no more need with gedmo traits
116
            // It is required that at least one field changes if you are using doctrine
117
            // otherwise the event listeners won't be called and the file is lost
118
            $this->updatedAt = new \DateTimeImmutable();
119
        }
120
    }
121
122
    public function getMediaFile(): ?File
123
    {
124
        return $this->mediaFile;
125
    }
126
127
    public function getMedia(): ?string
128
    {
129
        return $this->media;
130
    }
131
132
    public function setMedia($media): self
133
    {
134
        $this->media = $media;
135
136
        return $this;
137
    }
138
139 3
    public function getName(): ?string
140
    {
141 3
        return $this->name;
142
    }
143
144 3
    public function setName(?string $name): self
145
    {
146
        //if ($this->name !== null) { // sinon renommer l'media
147
        //throw new \Exception('Can\'t edit name.');
148
        //}
149
150 3
        $this->name = $name;
151
152 3
        return $this;
153
    }
154
155
    public function getRelativeDir(): ?string
156
    {
157
        return $this->relativeDir;
158
    }
159
160
    public function setRelativeDir($relativeDir): self
161
    {
162
        $this->relativeDir = $relativeDir;
163
164
        return $this;
165
    }
166
167
    public function getMimeType(): ?string
168
    {
169
        return $this->mimeType;
170
    }
171
172
    public function setMimeType($mimeType): self
173
    {
174
        $this->mimeType = $mimeType;
175
176
        return $this;
177
    }
178
179
    public function getSize()
180
    {
181
        return $this->size;
182
    }
183
184
    public function setSize($size): self
185
    {
186
        $this->size = $size;
187
188
        return $this;
189
    }
190
191
    public function setDimensions($dimensions): self
192
    {
193
        if (isset($dimensions[0])) {
194
            $this->width = (int) $dimensions[0];
195
        }
196
197
        if (isset($dimensions[1])) {
198
            $this->height = (int) $dimensions[1];
199
        }
200
201
        return $this;
202
    }
203
204
    public function getDimensions(): array
205
    {
206
        return [$this->width, $this->height];
207
    }
208
209
    public function getRatio(): float
210
    {
211
        return $this->height / $this->width;
212
    }
213
214
    public function getWidth()
215
    {
216
        return $this->width;
217
    }
218
219
    public function getHeight()
220
    {
221
        return $this->height;
222
    }
223
224
    public function getMainColor(): ?string
225
    {
226
        return $this->mainColor;
227
    }
228
229
    public function setMainColor(?string $mainColor): self
230
    {
231
        $this->mainColor = $mainColor;
232
233
        return $this;
234
    }
235
236
    public function setPageHasMedias($pageHasMedias)
237
    {
238
        $this->pageHasMedias = new ArrayCollection();
239
        foreach ($pageHasMedias as $pageHasMedia) {
240
            $this->addPageHasMedia($pageHasMedia);
241
        }
242
    }
243
244
    public function getPageHasMedias()
245
    {
246
        return $this->pageHasMedias;
247
    }
248
249
    public function addPageHasMedia(PageHasMedia $pageHasMedia)
250
    {
251
        $pageHasMedia->setMedia($this);
252
        $this->pageHasMedias[] = $pageHasMedia;
253
    }
254
255
    public function removePageHasMedia(PageHasMedia $pageHasMedia)
256
    {
257
        $this->pageHasMedias->removeElement($pageHasMedia);
258
    }
259
}
260