Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
65.96% covered (warning)
65.96%
62 / 94
65.71% covered (warning)
65.71%
23 / 35
CRAP
0.00% covered (danger)
0.00%
0 / 1
Document
65.96% covered (warning)
65.96%
62 / 94
65.71% covered (warning)
65.71%
23 / 35
196.33
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setName
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getCategories
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCategories
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 addCategory
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 removeCategory
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 getUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUrl
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFile
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getPinned
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPinned
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFavoritedUsers
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFavoritedUsers
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 addFavoritedUser
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 removeFavoritedUser
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 getUploadedFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setUploadedFile
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getIsLink
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setIsLink
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getThumbUploadedFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setThumbUploadedFile
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getThumbUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setThumbUrl
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getSize
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 setSize
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getType
66.67% covered (warning)
66.67%
8 / 12
0.00% covered (danger)
0.00%
0 / 1
10.37
 getTypeName
47.37% covered (danger)
47.37%
9 / 19
0.00% covered (danger)
0.00%
0 / 1
20.81
 getCreator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCreator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRemoveThumb
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setRemoveThumb
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This file is part of the MADIS - RGPD Management application.
5 *
6 * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22declare(strict_types=1);
23
24namespace App\Domain\Documentation\Model;
25
26use App\Application\Traits\Model\HistoryTrait;
27use App\Domain\Documentation\Dictionary\DocumentTypeDictionary;
28use App\Domain\User\Model\User;
29use Doctrine\Common\Collections\ArrayCollection;
30use Doctrine\Common\Collections\Collection;
31use Doctrine\ORM\Mapping as ORM;
32use Ramsey\Uuid\Uuid;
33use Ramsey\Uuid\UuidInterface;
34use Symfony\Component\HttpFoundation\File\UploadedFile;
35
36#[ORM\Entity]
37class Document
38{
39    use HistoryTrait;
40
41    #[ORM\Id]
42    #[ORM\Column(type: 'uuid', unique: true)]
43    private UuidInterface $id;
44
45    #[ORM\Column(type: 'string', length: 255)]
46    private ?string $name;
47
48    #[ORM\Column(type: 'text')]
49    private ?string $url = null;
50
51    #[ORM\Column(type: 'integer', nullable: false)]
52    private int $size;
53
54    #[ORM\Column(type: 'string', length: 255)]
55    private ?string $file;
56
57    private ?UploadedFile $uploadedFile;
58
59    private ?UploadedFile $thumbUploadedFile = null;
60
61    private ?bool $removeThumb = null;
62
63    #[ORM\Column(type: 'text', length: 255, nullable: true)]
64    private ?string $thumbUrl = null;
65
66    #[ORM\Column(type: 'boolean')]
67    private $pinned;
68
69    #[ORM\Column(type: 'boolean')]
70    private ?bool $isLink = false;
71
72    #[ORM\ManyToMany(targetEntity: "App\Domain\Documentation\Model\Category", mappedBy: 'documents')]
73    private array|Collection|null $categories;
74
75    #[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'favoriteDocuments')]
76    #[ORM\JoinTable(name: 'user_favorite_documents')]
77    private array|Collection|null $favoritedUsers;
78
79    #[ORM\ManyToOne(targetEntity: User::class, cascade: ['persist'], inversedBy: 'documents')]
80    #[ORM\JoinColumn(onDelete: 'SET NULL')]
81    private ?User $creator;
82
83    /**
84     * Document constructor.
85     *
86     * @throws \Exception
87     */
88    public function __construct()
89    {
90        $this->id             = Uuid::uuid4();
91        $this->categories     = new ArrayCollection();
92        $this->favoritedUsers = new ArrayCollection();
93    }
94
95    public function __toString(): string
96    {
97        return $this->name ?? '';
98    }
99
100    public function getId(): UuidInterface
101    {
102        return $this->id;
103    }
104
105    public function getName(): ?string
106    {
107        return $this->name;
108    }
109
110    public function setName(?string $name): Document
111    {
112        $this->name = $name;
113
114        return $this;
115    }
116
117    public function getCategories(): ?Collection
118    {
119        return $this->categories;
120    }
121
122    public function setCategories(?Collection $categories): Document
123    {
124        $this->categories = $categories;
125
126        return $this;
127    }
128
129    public function addCategory(Category $category): Document
130    {
131        if (null === $this->categories) {
132            $this->categories = new ArrayCollection();
133        }
134        if (!$this->categories->contains($category)) {
135            $this->categories->add($category);
136        }
137
138        return $this;
139    }
140
141    public function removeCategory(Category $category): Document
142    {
143        if (null !== $this->categories && $this->categories->contains($category)) {
144            $this->categories->removeElement($category);
145        }
146
147        return $this;
148    }
149
150    public function getUrl(): ?string
151    {
152        return $this->url;
153    }
154
155    public function setUrl(?string $url): Document
156    {
157        $this->url = $url;
158
159        return $this;
160    }
161
162    public function getFile(): ?string
163    {
164        return $this->file;
165    }
166
167    public function setFile(?string $file): Document
168    {
169        $this->file = $file;
170
171        return $this;
172    }
173
174    public function getPinned(): ?bool
175    {
176        return $this->pinned;
177    }
178
179    public function setPinned(?bool $pinned): Document
180    {
181        $this->pinned = $pinned;
182
183        return $this;
184    }
185
186    public function getFavoritedUsers(): ?Collection
187    {
188        return $this->favoritedUsers;
189    }
190
191    public function setFavoritedUsers(?array $favoritedUsers): Document
192    {
193        $this->favoritedUsers = $favoritedUsers;
194
195        return $this;
196    }
197
198    public function addFavoritedUser(User $user): Document
199    {
200        if (null === $this->favoritedUsers) {
201            $this->favoritedUsers = new ArrayCollection();
202        }
203        if (!$this->favoritedUsers->contains($user)) {
204            $this->favoritedUsers->add($user);
205        }
206
207        return $this;
208    }
209
210    public function removeFavoritedUser(User $user): Document
211    {
212        if (null !== $this->favoritedUsers && $this->favoritedUsers->contains($user)) {
213            $this->favoritedUsers->removeElement($user);
214        }
215
216        return $this;
217    }
218
219    public function getUploadedFile(): ?UploadedFile
220    {
221        return $this->uploadedFile;
222    }
223
224    public function setUploadedFile(?UploadedFile $uploadedFile): Document
225    {
226        $this->uploadedFile = $uploadedFile;
227
228        return $this;
229    }
230
231    public function getIsLink(): ?bool
232    {
233        return $this->isLink;
234    }
235
236    public function setIsLink(?bool $isLink): Document
237    {
238        $this->isLink = $isLink;
239
240        return $this;
241    }
242
243    public function getThumbUploadedFile(): ?UploadedFile
244    {
245        return $this->thumbUploadedFile;
246    }
247
248    public function setThumbUploadedFile(?UploadedFile $thumbUploadedFile): Document
249    {
250        $this->thumbUploadedFile = $thumbUploadedFile;
251
252        return $this;
253    }
254
255    public function getThumbUrl(): ?string
256    {
257        return $this->thumbUrl;
258    }
259
260    public function setThumbUrl(?string $thumbUrl): Document
261    {
262        $this->thumbUrl = $thumbUrl;
263
264        return $this;
265    }
266
267    public function getSize(): string
268    {
269        if ($this->size <= 0) {
270            return '';
271        }
272        $units = ['o', 'Ko', 'Mo', 'Go', 'To', 'Po', 'Eo', 'Zo', 'Yo'];
273        $pow   = floor(log((float) $this->size, 1024));
274        $size  = number_format($this->size / pow(1024, $pow), 2, ',', ' ') . ' ' . $units[$pow];
275
276        return $size;
277    }
278
279    public function setSize(int $size): Document
280    {
281        $this->size = $size;
282
283        return $this;
284    }
285
286    public function getType(): ?string
287    {
288        $extension = strtolower(pathinfo($this->getFile(), PATHINFO_EXTENSION));
289        if ('pdf' === $extension) {
290            return DocumentTypeDictionary::TYPE_PDF;
291        }
292        if ('mp4' === $extension || 'm4v' === $extension) {
293            return DocumentTypeDictionary::TYPE_MP4;
294        }
295        if ($this->getIsLink()) {
296            return DocumentTypeDictionary::TYPE_LINK;
297        }
298        if ('docx' === $extension) {
299            return DocumentTypeDictionary::TYPE_DOCX;
300        }
301        if ('png' === $extension || 'jpg' === $extension) {
302            return DocumentTypeDictionary::TYPE_IMG;
303        }
304
305        return null;
306    }
307
308    public function getTypeName(): ?string
309    {
310        $types     = DocumentTypeDictionary::getTypes();
311        $extension = strtolower(pathinfo($this->getFile(), PATHINFO_EXTENSION));
312
313        if ('pdf' === $extension) {
314            return $types[DocumentTypeDictionary::TYPE_PDF];
315        }
316        if (in_array($extension, ['mp4', 'mov', 'wmv', 'avi', 'mpg', 'ogv', 'ogg', 'webm'])) {
317            return $types[DocumentTypeDictionary::TYPE_MP4];
318        }
319        if ($this->getIsLink()) {
320            return $types[DocumentTypeDictionary::TYPE_LINK];
321        }
322        if (in_array($extension, ['docx', 'doc', 'odt'])) {
323            return $types[DocumentTypeDictionary::TYPE_DOCX];
324        }
325        if (in_array($extension, ['png', 'jpg'])) {
326            return $types[DocumentTypeDictionary::TYPE_IMG];
327        }
328        if (in_array($extension, ['mp3', 'm4a', 'ogg', 'wav'])) {
329            return $types[DocumentTypeDictionary::TYPE_AUDIO];
330        }
331        if (in_array($extension, ['ppt', 'pptx', 'odp'])) {
332            return $types[DocumentTypeDictionary::TYPE_PPT];
333        }
334        if (in_array($extension, ['xls', 'xlsx', 'xlsm', 'ods'])) {
335            return $types[DocumentTypeDictionary::TYPE_EXCEL];
336        }
337
338        return null;
339    }
340
341    public function getCreator(): ?User
342    {
343        return $this->creator;
344    }
345
346    public function setCreator(?User $creator): void
347    {
348        $this->creator = $creator;
349    }
350
351    public function getRemoveThumb(): ?bool
352    {
353        return $this->removeThumb;
354    }
355
356    public function setRemoveThumb(?bool $removeThumb): void
357    {
358        $this->removeThumb = $removeThumb;
359    }
360}