Passed
Push — master ( 9d1dd5...9dc987 )
by Dev
04:35
created

MediaTrait   B

Complexity

Total Complexity 50

Size/Duplication

Total Lines 314
Duplicated Lines 0 %

Test Coverage

Coverage 8.49%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 83
c 2
b 0
f 0
dl 0
loc 314
ccs 9
cts 106
cp 0.0849
rs 8.4
wmc 50

33 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A removeMainImageFromPages() 0 4 2
A setMediaFile() 0 8 2
A setMimeType() 0 5 1
A getMimeType() 0 3 1
A getPageHasMedias() 0 3 1
A setMainColor() 0 5 1
A getMainImagePages() 0 3 1
A getSlug() 0 14 3
A setName() 0 5 1
A setMedia() 0 5 1
A getNameByLocale() 0 5 1
A getMedia() 0 3 1
A getHeight() 0 3 1
A setNames() 0 5 1
A setSlug() 0 11 2
A getName() 0 7 4
A getSize() 0 3 1
A getMediaFile() 0 3 1
A getDimensions() 0 3 1
A getNames() 0 3 3
A getRelativeDir() 0 3 1
A setRelativeDir() 0 5 1
A removePageHasMedia() 0 3 1
A setDimensions() 0 11 3
A addPageHasMedia() 0 4 1
A setSize() 0 5 1
A setPageHasMedias() 0 5 2
A getMainColor() 0 3 1
A getWidth() 0 3 1
A getRatio() 0 3 1
A getFullPathWebP() 0 5 3
A getFullPath() 0 5 3

How to fix   Complexity   

Complex Class

Complex classes like MediaTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MediaTrait, and based on these observations, apply Extract Interface, too.

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, $force = false)
105
    {
106
        if (true === $force) {
107
            $this->slug = $slug;
108
109
            return $this;
110
        }
111
112
        $this->setMedia($slug);
113
114
        return $this;
115
    }
116
117
    public function getSlug(): string
118
    {
119
        if ($this->slug) {
120
            return $this->slug;
121
        }
122
123
        if ($this->media) {
124
            return $this->slug = preg_replace('/\\.[^.\\s]{3,4}$/', '', $this->media);
125
        }
126
127
        $slugify = new Slugify();
128
        $this->slug = $slugify->slugify($this->getName()); //Urlizer::urlize($this->getName());
129
130
        return $this->slug;
131
    }
132
133
    public function setMediaFile(?File $media = null): void
134
    {
135
        $this->mediaFile = $media;
136
137
        if (null !== $media) { //normaly no more need with gedmo traits
138
            // It is required that at least one field changes if you are using doctrine
139
            // otherwise the event listeners won't be called and the file is lost
140
            $this->updatedAt = new \DateTimeImmutable();
141
        }
142
    }
143
144
    public function getMediaFile(): ?File
145
    {
146
        return $this->mediaFile;
147
    }
148
149
    public function getMedia(): ?string
150
    {
151
        return $this->media;
152
    }
153
154
    public function setMedia($media): self
155
    {
156
        $this->media = $media;
157
158
        return $this;
159
    }
160
161 3
    public function getName($getLocalized = null, $onlyLocalized = false): ?string
162
    {
163 3
        $names = $this->getNames(true);
164
165 3
        return $getLocalized ?
166
            (isset($names[$getLocalized]) ? $names[$getLocalized] : ($onlyLocalized ? null : $this->name))
167 3
            : $this->name;
168
    }
169
170 3
    public function getNames($yaml = false)
171
    {
172 3
        return true === $yaml && null !== $this->names ? Yaml::parse($this->names) : $this->names;
173
    }
174
175
    public function setNames(?string $names): self
176
    {
177
        $this->name = $names;
178
179
        return $this;
180
    }
181
182
    public function getNameByLocale($locale)
183
    {
184
        $names = $this->getNames(true);
185
186
        return $names[$locale] ?? $this->getName();
187
    }
188
189 3
    public function setName(?string $name): self
190
    {
191 3
        $this->name = $name;
192
193 3
        return $this;
194
    }
195
196
    public function getRelativeDir(): ?string
197
    {
198
        return $this->relativeDir;
199
    }
200
201
    public function setRelativeDir($relativeDir): self
202
    {
203
        $this->relativeDir = $relativeDir;
204
205
        return $this;
206
    }
207
208
    public function getMimeType(): ?string
209
    {
210
        return $this->mimeType;
211
    }
212
213
    public function setMimeType($mimeType): self
214
    {
215
        $this->mimeType = $mimeType;
216
217
        return $this;
218
    }
219
220
    public function getSize()
221
    {
222
        return $this->size;
223
    }
224
225
    public function setSize($size): self
226
    {
227
        $this->size = $size;
228
229
        return $this;
230
    }
231
232
    public function setDimensions($dimensions): self
233
    {
234
        if (isset($dimensions[0])) {
235
            $this->width = (int) $dimensions[0];
236
        }
237
238
        if (isset($dimensions[1])) {
239
            $this->height = (int) $dimensions[1];
240
        }
241
242
        return $this;
243
    }
244
245
    public function getDimensions(): array
246
    {
247
        return [$this->width, $this->height];
248
    }
249
250
    public function getRatio(): float
251
    {
252
        return $this->height / $this->width;
253
    }
254
255
    public function getWidth()
256
    {
257
        return $this->width;
258
    }
259
260
    public function getHeight()
261
    {
262
        return $this->height;
263
    }
264
265
    public function getMainColor(): ?string
266
    {
267
        return $this->mainColor;
268
    }
269
270
    public function setMainColor(?string $mainColor): self
271
    {
272
        $this->mainColor = $mainColor;
273
274
        return $this;
275
    }
276
277
    public function setPageHasMedias($pageHasMedias)
278
    {
279
        $this->pageHasMedias = new ArrayCollection();
280
        foreach ($pageHasMedias as $pageHasMedia) {
281
            $this->addPageHasMedia($pageHasMedia);
282
        }
283
    }
284
285
    public function getPageHasMedias()
286
    {
287
        return $this->pageHasMedias;
288
    }
289
290
    public function addPageHasMedia(PageHasMedia $pageHasMedia)
291
    {
292
        $pageHasMedia->setMedia($this);
293
        $this->pageHasMedias[] = $pageHasMedia;
294
    }
295
296
    public function removePageHasMedia(PageHasMedia $pageHasMedia)
297
    {
298
        $this->pageHasMedias->removeElement($pageHasMedia);
299
    }
300
301
    public function getMainImagePages()
302
    {
303
        return $this->mainImagePages;
304
    }
305
306
    /**
307
     * @ORM\PreRemove
308
     */
309
    public function removeMainImageFromPages()
310
    {
311
        foreach ($this->mainImagePages as $page) {
312
            $page->setMainImage(null);
313
        }
314
    }
315
316
    public function getFullPath(): ?string
317
    {
318
        return null !== $this->media
319
            ? '/'.$this->getRelativeDir().($this->getMedia() ? '/'.$this->getMedia() : '')
320
            : null;
321
        // todo : check if / is needed
322
    }
323
324
    public function getFullPathWebP(): ?string
325
    {
326
        return null !== $this->media
327
            ? '/'.$this->getRelativeDir().($this->getSlug() ? '/'.$this->getSlug().'.webp' : '')
328
            : null;
329
        // todo : check if / is needed
330
    }
331
}
332