Passed
Push — master ( ae4102...364b44 )
by Dev
11:37
created

MediaTrait::getRatio()   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
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PiedWeb\CMSBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Vich\UploaderBundle\Mapping\Annotation as Vich;
7
use Symfony\Component\HttpFoundation\File\File;
8
use Gedmo\Sluggable\Util\Urlizer;
9
use Cocur\Slugify\Slugify;
10
use Gedmo\Mapping\Annotation as Gedmo;
11
use Doctrine\Common\Collections\ArrayCollection;
12
use Gedmo\Timestampable\Traits\TimestampableEntity;
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="media",
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 getSlug(): string
91
    {
92
        if (!$this->slug) {
93
            $slugify = new Slugify();
94
95
            return $this->slug = $slugify->slugify($this->getName()); //Urlizer::urlize($this->getName());
96
        }
97
98
        return $this->slug;
99
    }
100
101
    public function setMediaFile(?File $media = null): void
102
    {
103
        $this->mediaFile = $media;
104
105
        if (null !== $media) { //normaly no more need with gedmo traits
106
            // It is required that at least one field changes if you are using doctrine
107
            // otherwise the event listeners won't be called and the file is lost
108
            $this->updatedAt = new \DateTimeImmutable();
109
        }
110
    }
111
112
    public function getMediaFile(): ?File
113
    {
114
        return $this->mediaFile;
115
    }
116
117
    public function getMedia(): ?string
118
    {
119
        return $this->media;
120
    }
121
122
    public function setMedia($media): self
123
    {
124
        $this->media = $media;
125
126
        return $this;
127
    }
128
129 3
    public function getName(): ?string
130
    {
131 3
        return $this->name;
132
    }
133
134 3
    public function setName(?string $name): self
135
    {
136
        //if ($this->name !== null) { // sinon renommer l'media
137
        //throw new \Exception('Can\'t edit name.');
138
        //}
139
140 3
        $this->name = $name;
141
142 3
        return $this;
143
    }
144
145
    public function getRelativeDir(): ?string
146
    {
147
        return $this->relativeDir;
148
    }
149
150
    public function setRelativeDir($relativeDir): self
151
    {
152
        $this->relativeDir = $relativeDir;
153
154
        return $this;
155
    }
156
157
    public function getMimeType(): ?string
158
    {
159
        return $this->mimeType;
160
    }
161
162
    public function setMimeType($mimeType): self
163
    {
164
        $this->mimeType = $mimeType;
165
166
        return $this;
167
    }
168
169
    public function getSize()
170
    {
171
        return $this->size;
172
    }
173
174
    public function setSize($size): self
175
    {
176
        $this->size = $size;
177
178
        return $this;
179
    }
180
181
    public function setDimensions($dimensions): self
182
    {
183
        if (isset($dimensions[0])) {
184
            $this->width = (int) $dimensions[0];
185
        }
186
187
        if (isset($dimensions[1])) {
188
            $this->height = (int) $dimensions[1];
189
        }
190
191
        return $this;
192
    }
193
194
    public function getDimensions(): array
195
    {
196
        return [$this->width, $this->height];
197
    }
198
199
    public function getRatio(): float
200
    {
201
        return $this->height / $this->width;
202
    }
203
204
    public function getWidth()
205
    {
206
        return $this->width;
207
    }
208
209
    public function getHeight()
210
    {
211
        return $this->height;
212
    }
213
214
    public function getMainColor(): ?string
215
    {
216
        return $this->mainColor;
217
    }
218
219
    public function setMainColor(?string $mainColor): self
220
    {
221
        $this->mainColor = $mainColor;
222
223
        return $this;
224
    }
225
226
    public function setPageHasMedias($pageHasMedias)
227
    {
228
        $this->pageHasMedias = new ArrayCollection();
229
        foreach ($pageHasMedias as $pageHasMedia) {
230
            $this->addPageHasMedia($pageHasMedia);
231
        }
232
    }
233
234
    public function getPageHasMedias()
235
    {
236
        return $this->pageHasMedias;
237
    }
238
239
    public function addPageHasMedia(PageHasMedia $pageHasMedia)
240
    {
241
        $pageHasMedia->setMedia($this);
242
        $this->pageHasMedias[] = $pageHasMedia;
243
    }
244
245
    public function removePageHasMedia(PageHasMedia $pageHasMedia)
246
    {
247
        $this->pageHasMedias->removeElement($pageHasMedia);
248
    }
249
}
250