Passed
Push — master ( af608e...293c09 )
by Dev
11:05
created

MediaTrait::getMainColor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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
12
trait MediaTrait
13
{
14
    /**
15
     * @ORM\Column(type="string", length=50)
16
     */
17
    private $mimeType;
18
19
    /**
20
     * @ORM\Column(type="string", length=255)
21
     */
22
    private $relativeDir;
23
24
    /**
25
     * @ORM\Column(type="integer")
26
     */
27
    private $size;
28
29
    /**
30
     * @ORM\Column(type="integer", nullable=true)
31
     */
32
    private $height;
33
34
    /**
35
     * @ORM\Column(type="integer", nullable=true)
36
     */
37
    private $width;
38
39
    /**
40
     * @ORM\Column(type="string", nullable=true)
41
     */
42
    private $mainColor;
43
44
    /**
45
     * @ORM\Column(type="string", length=255)
46
     */
47
    private $media;
48
49
    /**
50
     * NOTE: This is not a mapped field of entity metadata, just a simple property.
51
     *
52
     * @Vich\UploadableField(mapping="media_media", fileNameProperty="media", mimeType="mimeType", size="size", dimensions="dimensions")
53
     *
54
     * @var File
55
     */
56
    private $mediaFile;
57
58
    /**
59
     * @Gedmo\Translatable
60
     * @ORM\Column(type="string", length=100, unique=true)
61
     */
62
    private $name;
63
64
    private $slug;
65
66
    /**
67
     * @ORM\OneToMany(targetEntity="PiedWeb\CMSBundle\Entity\PageHasMedia", mappedBy="media",cascade={"all"}, orphanRemoval=true)
68
     */
69
    private $pageHasMedias;
70
71
    public function __toString()
72
    {
73
        return $this->name.' ';
74
    }
75
76
    public function getSlug(): string
77
    {
78
        if (!$this->slug) {
79
            $slugify = new Slugify();
80
81
            return $this->slug = $slugify->slugify($this->getName()); //Urlizer::urlize($this->getName());
82
        }
83
84
        return $this->slug;
85
    }
86
87
    public function setMediaFile(?File $media = null): void
88
    {
89
        $this->mediaFile = $media;
90
91
        if (null !== $media) { //normaly no more need with gedmo traits
92
            // It is required that at least one field changes if you are using doctrine
93
            // otherwise the event listeners won't be called and the file is lost
94
            $this->updatedAt = new \DateTimeImmutable();
0 ignored issues
show
Bug Best Practice introduced by
The property updatedAt does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
95
        }
96
    }
97
98
    public function getMediaFile(): ?File
99
    {
100
        return $this->mediaFile;
101
    }
102
103
    public function getMedia(): ?string
104
    {
105
        return $this->media;
106
    }
107
108
    public function setMedia($media): self
109
    {
110
        $this->media = $media;
111
112
        return $this;
113
    }
114
115
    public function getName(): ?string
116
    {
117
        return $this->name;
118
    }
119
120
    public function setName(?string $name): self
121
    {
122
        //if ($this->name !== null) { // sinon renommer l'media
123
        //throw new \Exception('Can\'t edit name.');
124
        //}
125
126
        $this->name = $name;
127
128
        return $this;
129
    }
130
131
    public function getRelativeDir(): ?string
132
    {
133
        return $this->relativeDir;
134
    }
135
136
    public function setRelativeDir($relativeDir): self
137
    {
138
        $this->relativeDir = $relativeDir;
139
140
        return $this;
141
    }
142
143
    public function getMimeType(): ?string
144
    {
145
        return $this->mimeType;
146
    }
147
148
    public function setMimeType($mimeType): self
149
    {
150
        $this->mimeType = $mimeType;
151
152
        return $this;
153
    }
154
155
    public function getSize()
156
    {
157
        return $this->size;
158
    }
159
160
    public function setSize($size): self
161
    {
162
        $this->size = $size;
163
164
        return $this;
165
    }
166
167
    public function setDimensions($dimensions): self
168
    {
169
        if (isset($dimensions[0])) {
170
            $this->width = (int) $dimensions[0];
171
        }
172
173
        if (isset($dimensions[1])) {
174
            $this->height = (int) $dimensions[1];
175
        }
176
177
        return $this;
178
    }
179
180
    public function getDimensions(): array
181
    {
182
        return [$this->width, $this->height];
183
    }
184
185
    public function getWidth()
186
    {
187
        return $this->width;
188
    }
189
190
    public function getHeight()
191
    {
192
        return $this->height;
193
    }
194
195
    public function getMainColor(): ?string
196
    {
197
        return $this->mainColor;
198
    }
199
200
    public function setMainColor(?string $mainColor): self
201
    {
202
        $this->mainColor = $mainColor;
203
204
        return $this;
205
    }
206
207
208
    public function setPageHasMedias($pageHasMedias)
209
    {
210
        $this->pageHasMedias = new ArrayCollection();
0 ignored issues
show
Bug introduced by
The type PiedWeb\CMSBundle\Entity\ArrayCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
211
        foreach ($pageHasMedias as $pageHasMedia) {
212
            $this->addPageHasMedia($pageHasMedia);
213
        }
214
    }
215
216
    public function getPageHasMedias()
217
    {
218
        return $this->pageHasMedias;
219
    }
220
221
    public function addPageHasMedia(PageHasMedia $pageHasMedia)
222
    {
223
        $pageHasMedia->setPage($this);
0 ignored issues
show
Bug introduced by
$this of type PiedWeb\CMSBundle\Entity\MediaTrait is incompatible with the type PiedWeb\CMSBundle\Entity\PageInterface|null expected by parameter $page of PiedWeb\CMSBundle\Entity\PageHasMedia::setPage(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

223
        $pageHasMedia->setPage(/** @scrutinizer ignore-type */ $this);
Loading history...
224
        $this->pageHasMedias[] = $pageHasMedia;
225
    }
226
227
    public function removePageHasMedia(PageHasMedia $pageHasMedia)
228
    {
229
        $this->pageHasMedias->removeElement($pageHasMedia);
230
    }
231
}
232