Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.39% covered (warning)
67.39%
62 / 92
71.43% covered (warning)
71.43%
25 / 35
CRAP
0.00% covered (danger)
0.00%
0 / 1
Document
67.39% covered (warning)
67.39%
62 / 92
71.43% covered (warning)
71.43%
25 / 35
179.70
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
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
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 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
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 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/**
37 * @ORM\Entity
38 */
39class Document
40{
41    use HistoryTrait;
42    /**
43     * @ORM\Id()
44     *
45     * @ORM\Column(type="uuid")
46     *
47     * @var UuidInterface
48     */
49    private $id;
50
51    /**
52     * @ORM\Column(type="string", length=255)
53     *
54     * @var string|null
55     */
56    private $name;
57
58    /**
59     * @ORM\Column(type="text")
60     *
61     * @var string|null
62     */
63    private $url;
64
65    /**
66     * @ORM\Column(type="integer", nullable=true)
67     *
68     * @var int|null
69     */
70    private $size;
71
72    /**
73     * @ORM\Column(type="string", length=255)
74     *
75     * @var string|null
76     */
77    private $file;
78
79    /**
80     * @var UploadedFile|null
81     */
82    private $uploadedFile;
83
84    /**
85     * @var UploadedFile|null
86     */
87    private $thumbUploadedFile;
88
89    /**
90     * @var bool|null
91     */
92    private $removeThumb;
93
94    /**
95     * @var string|null
96     *
97     * @ORM\Column(type="text", length=255, nullable=true)
98     */
99    private $thumbUrl;
100
101    /**
102     * @ORM\Column(type="boolean")
103     *
104     * @var bool|null
105     */
106    private $pinned;
107
108    /**
109     * @ORM\Column(type="boolean")
110     *
111     * @var bool|null
112     */
113    private $isLink;
114
115    /**
116     * @ORM\ManyToMany(targetEntity="Category", inversedBy="documents")
117     *
118     * @ORM\JoinTable(name="document_categories",
119     *      joinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")},
120     *      inverseJoinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id")}
121     *      )
122     *
123     * @ORM\OrderBy({"name" = "ASC"})
124     *
125     * @var Collection|Category[]
126     */
127    private $categories;
128
129    /**
130     * @ORM\ManyToMany(targetEntity="App\Domain\User\Model\User", inversedBy="favoriteDocuments")
131     *
132     * @ORM\JoinTable(name="user_favorite_documents",
133     *      joinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id")},
134     *      inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
135     *      )
136     *
137     * @var Collection|User[]
138     */
139    private $favoritedUsers;
140
141    /**
142     * @ORM\ManyToOne(targetEntity="App\Domain\User\Model\User", cascade={"persist"})
143     *
144     * @ORM\JoinColumn(onDelete="SET NULL")
145     *
146     * @var User|null
147     */
148    private $creator;
149
150    /**
151     * Answer constructor.
152     *
153     * @throws \Exception
154     */
155    public function __construct()
156    {
157        $this->id = Uuid::uuid4();
158    }
159
160    public function __toString()
161    {
162        return $this->name;
163    }
164
165    public function getId(): UuidInterface
166    {
167        return $this->id;
168    }
169
170    public function getName(): ?string
171    {
172        return $this->name;
173    }
174
175    public function setName(?string $name): Document
176    {
177        $this->name = $name;
178
179        return $this;
180    }
181
182    public function getCategories(): ?Collection
183    {
184        return $this->categories;
185    }
186
187    public function setCategories(?Collection $categories): Document
188    {
189        $this->categories = $categories;
190
191        return $this;
192    }
193
194    public function addCategory(Category $category): Document
195    {
196        if (null === $this->categories) {
197            $this->categories = new ArrayCollection();
198        }
199        if (!$this->categories->contains($category)) {
200            $this->categories->add($category);
201        }
202
203        return $this;
204    }
205
206    public function removeCategory(Category $category): Document
207    {
208        if (null !== $this->categories && $this->categories->contains($category)) {
209            $this->categories->removeElement($category);
210        }
211
212        return $this;
213    }
214
215    public function getUrl(): ?string
216    {
217        return $this->url;
218    }
219
220    public function setUrl(?string $url): Document
221    {
222        $this->url = $url;
223
224        return $this;
225    }
226
227    public function getFile(): ?string
228    {
229        return $this->file;
230    }
231
232    public function setFile(?string $file): Document
233    {
234        $this->file = $file;
235
236        return $this;
237    }
238
239    public function getPinned(): ?bool
240    {
241        return $this->pinned;
242    }
243
244    public function setPinned(?bool $pinned): Document
245    {
246        $this->pinned = $pinned;
247
248        return $this;
249    }
250
251    public function getFavoritedUsers(): ?Collection
252    {
253        return $this->favoritedUsers;
254    }
255
256    public function setFavoritedUsers(?array $favoritedUsers): Document
257    {
258        $this->favoritedUsers = $favoritedUsers;
259
260        return $this;
261    }
262
263    public function addFavoritedUser(User $user): Document
264    {
265        if (null === $this->favoritedUsers) {
266            $this->favoritedUsers = new ArrayCollection();
267        }
268        if (!$this->favoritedUsers->contains($user)) {
269            $this->favoritedUsers->add($user);
270        }
271
272        return $this;
273    }
274
275    public function removeFavoritedUser(User $user): Document
276    {
277        if (null !== $this->favoritedUsers && $this->favoritedUsers->contains($user)) {
278            $this->favoritedUsers->removeElement($user);
279        }
280
281        return $this;
282    }
283
284    public function getUploadedFile(): ?UploadedFile
285    {
286        return $this->uploadedFile;
287    }
288
289    public function setUploadedFile(?UploadedFile $uploadedFile): Document
290    {
291        $this->uploadedFile = $uploadedFile;
292
293        return $this;
294    }
295
296    public function getIsLink(): ?bool
297    {
298        return $this->isLink;
299    }
300
301    public function setIsLink(?bool $isLink): Document
302    {
303        $this->isLink = $isLink;
304
305        return $this;
306    }
307
308    public function getThumbUploadedFile(): ?UploadedFile
309    {
310        return $this->thumbUploadedFile;
311    }
312
313    public function setThumbUploadedFile(?UploadedFile $thumbUploadedFile): Document
314    {
315        $this->thumbUploadedFile = $thumbUploadedFile;
316
317        return $this;
318    }
319
320    public function getThumbUrl(): ?string
321    {
322        return $this->thumbUrl;
323    }
324
325    public function setThumbUrl(?string $thumbUrl): Document
326    {
327        $this->thumbUrl = $thumbUrl;
328
329        return $this;
330    }
331
332    public function getSize(): ?string
333    {
334        if ($this->size <= 0) {
335            return '';
336        }
337        $units = ['o', 'Ko', 'Mo', 'Go', 'To', 'Po', 'Eo', 'Zo', 'Yo'];
338        $pow   = floor(log((float) $this->size, 1024));
339        $size  = number_format($this->size / pow(1024, $pow), 2, ',', ' ') . ' ' . $units[$pow];
340
341        return $size;
342    }
343
344    public function setSize(?int $size): Document
345    {
346        $this->size = $size;
347
348        return $this;
349    }
350
351    public function getType(): ?string
352    {
353        $extension = strtolower(pathinfo($this->getFile(), PATHINFO_EXTENSION));
354        if ('pdf' === $extension) {
355            return DocumentTypeDictionary::TYPE_PDF;
356        }
357        if ('mp4' === $extension || 'm4v' === $extension) {
358            return DocumentTypeDictionary::TYPE_MP4;
359        }
360        if ($this->getIsLink()) {
361            return DocumentTypeDictionary::TYPE_LINK;
362        }
363        if ('docx' === $extension) {
364            return DocumentTypeDictionary::TYPE_DOCX;
365        }
366        if ('png' === $extension || 'jpg' === $extension) {
367            return DocumentTypeDictionary::TYPE_IMG;
368        }
369
370        return null;
371    }
372
373    public function getTypeName(): ?string
374    {
375        $types     = DocumentTypeDictionary::getTypes();
376        $extension = strtolower(pathinfo($this->getFile(), PATHINFO_EXTENSION));
377
378        if ('pdf' === $extension) {
379            return $types[DocumentTypeDictionary::TYPE_PDF];
380        }
381        if (in_array($extension, ['mp4', 'mov', 'wmv', 'avi', 'mpg', 'ogv', 'ogg', 'webm'])) {
382            return $types[DocumentTypeDictionary::TYPE_MP4];
383        }
384        if ($this->getIsLink()) {
385            return $types[DocumentTypeDictionary::TYPE_LINK];
386        }
387        if (in_array($extension, ['docx', 'doc', 'odt'])) {
388            return $types[DocumentTypeDictionary::TYPE_DOCX];
389        }
390        if (in_array($extension, ['png', 'jpg'])) {
391            return $types[DocumentTypeDictionary::TYPE_IMG];
392        }
393        if (in_array($extension, ['mp3', 'm4a', 'ogg', 'wav'])) {
394            return $types[DocumentTypeDictionary::TYPE_AUDIO];
395        }
396        if (in_array($extension, ['ppt', 'pptx', 'odp'])) {
397            return $types[DocumentTypeDictionary::TYPE_PPT];
398        }
399        if (in_array($extension, ['xls', 'xlsx', 'xlsm', 'ods'])) {
400            return $types[DocumentTypeDictionary::TYPE_EXCEL];
401        }
402
403        return null;
404    }
405
406    public function getCreator(): ?User
407    {
408        return $this->creator;
409    }
410
411    public function setCreator(?User $creator): void
412    {
413        $this->creator = $creator;
414    }
415
416    public function getRemoveThumb(): ?bool
417    {
418        return $this->removeThumb;
419    }
420
421    public function setRemoveThumb(?bool $removeThumb): void
422    {
423        $this->removeThumb = $removeThumb;
424    }
425}