Passed
Push — master ( b3f573...f22e85 )
by Yannick
06:55 queued 13s
created

ResourceFile::getHeight()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Entity;
8
9
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
10
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
11
use ApiPlatform\Metadata\ApiFilter;
12
use ApiPlatform\Metadata\ApiResource;
13
use ApiPlatform\Metadata\Get;
14
use ApiPlatform\Metadata\GetCollection;
15
use ApiPlatform\Metadata\Post;
16
use ApiPlatform\Serializer\Filter\PropertyFilter;
17
use Chamilo\CoreBundle\Controller\CreateResourceFileAction;
18
use DateTime;
19
use DateTimeImmutable;
20
use Doctrine\ORM\Mapping as ORM;
21
use Gedmo\Mapping\Annotation as Gedmo;
22
use Gedmo\Timestampable\Traits\TimestampableEntity;
23
use Stringable;
24
use Symfony\Component\HttpFoundation\File\File;
25
use Symfony\Component\HttpFoundation\File\UploadedFile;
26
use Symfony\Component\Serializer\Annotation\Groups;
27
use Symfony\Component\Validator\Constraints as Assert;
28
use Vich\UploaderBundle\Mapping\Annotation as Vich;
29
30
//
31
// *     attributes={"security"="is_granted('ROLE_ADMIN')"},
32
/**
33
 * @Vich\Uploadable
34
 */
35
#[ApiResource(
36
    types: ['http://schema.org/MediaObject'],
37
    operations: [
38
        new Get(),
39
        new Post(
40
            controller: CreateResourceFileAction::class,
41
            openapiContext: [
42
                'requestBody' => [
43
                    'content' => [
44
                        'multipart/form-data' => [
45
                            'schema' => [
46
                                'type' => 'object',
47
                                'properties' => [
48
                                    'file' => [
49
                                        'type' => 'string',
50
                                        'format' => 'binary',
51
                                    ],
52
                                ],
53
                            ],
54
                        ],
55
                    ],
56
                ],
57
            ],
58
            security: 'is_granted(\'ROLE_USER\')',
59
            validationContext: [
60
                'groups' => ['Default', 'media_object_create', 'document:write'],
61
            ],
62
            deserialize: false
63
        ),
64
        new GetCollection(),
65
    ],
66
    normalizationContext: [
67
        'groups' => [
68
            'resource_file:read',
69
            'resource_node:read',
70
            'document:read',
71
            'media_object_read',
72
            'message:read',
73
        ],
74
    ]
75
)]
76
#[ORM\Table(name: 'resource_file')]
77
#[ORM\Entity]
78
#[ApiFilter(filterClass: PropertyFilter::class)]
79
#[ApiFilter(filterClass: SearchFilter::class, properties: ['name' => 'partial'])]
80
#[ApiFilter(filterClass: OrderFilter::class, properties: ['id', 'name', 'size', 'updatedAt'])]
81
class ResourceFile implements Stringable
82
{
83
    use TimestampableEntity;
84
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read'])]
85
    #[ORM\Id]
86
    #[ORM\Column(type: 'integer')]
87
    #[ORM\GeneratedValue]
88
    protected ?int $id = null;
89
    #[Assert\NotBlank]
90
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read'])]
91
    #[ORM\Column(type: 'string', length: 255)]
92
    protected ?string $title = null;
93
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read'])]
94
    #[ORM\Column(type: 'text', nullable: true)]
95
    protected ?string $mimeType = null;
96
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read'])]
97
    #[ORM\Column(type: 'text', nullable: true)]
98
    protected ?string $originalName = null;
99
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read'])]
100
    #[ORM\Column(type: 'simple_array', nullable: true)]
101
    protected ?array $dimensions;
102
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read'])]
103
    #[ORM\Column(type: 'integer')]
104
    protected ?int $size = 0;
105
106
    /**
107
     * @Vich\UploadableField(
108
     *     mapping="resources",
109
     *     fileNameProperty="title",
110
     *     size="size",
111
     *     mimeType="mimeType",
112
     *     originalName="originalName",
113
     *     dimensions="dimensions"
114
     * )
115
     */
116
    //    #[Vich\UploadableField(
117
    //        mapping: 'resources',
118
    //        fileNameProperty: 'title',
119
    //        size: 'size',
120
    //        mimeType: 'mimeType',
121
    //        originalName: 'originalName',
122
    //        dimensions: 'dimensions'
123
    //    )]
124
    protected ?File $file = null;
125
    #[ORM\Column(name: 'crop', type: 'string', length: 255, nullable: true)]
126
    protected ?string $crop = null;
127
    #[ORM\OneToOne(mappedBy: 'resourceFile', targetEntity: ResourceNode::class)]
128
    protected ResourceNode $resourceNode;
129
130
    /**
131
     * @var string[]
132
     */
133
    #[ORM\Column(type: 'array', nullable: true)]
134
    protected ?array $metadata = [];
135
    #[Groups(['message:read'])]
136
    protected ?bool $audio = null;
137
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read'])]
138
    protected ?bool $image = null;
139
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read'])]
140
    protected ?bool $video = null;
141
    #[Groups(['resource_file:read', 'resource_node:read', 'document:read', 'message:read'])]
142
    protected ?bool $text = null;
143
    #[ORM\Column(name: 'description', type: 'text', nullable: true)]
144
    protected ?string $description = null;
145
146
    /**
147
     * @var DateTime|DateTimeImmutable
148
     */
149
    #[Gedmo\Timestampable(on: 'update')]
150
    #[ORM\Column(type: 'datetime')]
151
    protected $updatedAt;
152
    public function __construct()
153
    {
154
        $this->size = 0;
155
        $this->metadata = [];
156
        $this->dimensions = [];
157
    }
158
    public function __toString(): string
159
    {
160
        return $this->getOriginalName();
161
    }
162
    public function isText(): bool
163
    {
164
        $mimeType = $this->getMimeType();
165
166
        return str_contains($mimeType, 'text');
167
    }
168
    public function isImage(): bool
169
    {
170
        $mimeType = $this->getMimeType();
171
172
        return str_contains($mimeType, 'image');
173
    }
174
    public function isVideo(): bool
175
    {
176
        $mimeType = $this->getMimeType();
177
178
        return str_contains($mimeType, 'video');
179
    }
180
    public function isAudio(): bool
181
    {
182
        $mimeType = $this->getMimeType();
183
184
        return str_contains($mimeType, 'audio');
185
    }
186
    public function getTitle(): ?string
187
    {
188
        return $this->title;
189
    }
190
    public function setTitle(?string $title): self
191
    {
192
        $this->title = $title;
193
194
        return $this;
195
    }
196
    public function getCrop(): ?string
197
    {
198
        return $this->crop;
199
    }
200
201
    /**
202
     * $crop example: 100,100,100,100 = width,height,x,y.
203
     */
204
    public function setCrop(string $crop): self
205
    {
206
        $this->crop = $crop;
207
208
        return $this;
209
    }
210
    public function getSize(): ?int
211
    {
212
        return $this->size;
213
    }
214
    public function setSize(?int $size): self
215
    {
216
        $this->size = $size;
217
218
        return $this;
219
    }
220
    public function getResourceNode(): ResourceNode
221
    {
222
        return $this->resourceNode;
223
    }
224
    public function setResourceNode(ResourceNode $resourceNode): self
225
    {
226
        $this->resourceNode = $resourceNode;
227
228
        return $this;
229
    }
230
    /*public function isEnabled(): bool
231
        {
232
            return $this->enabled;
233
        }
234
235
        public function setEnabled(bool $enabled): self
236
        {
237
            $this->enabled = $enabled;
238
239
            return $this;
240
        }*/
241
    public function getId(): ?int
242
    {
243
        return $this->id;
244
    }
245
    /*public function getDescription(): string
246
        {
247
            return $this->description;
248
        }
249
250
        public function setDescription(string $description): self
251
        {
252
            $this->description = $description;
253
254
            return $this;
255
        }*/
256
    public function getMimeType(): ?string
257
    {
258
        return $this->mimeType;
259
    }
260
    public function setMimeType(?string $mimeType): self
261
    {
262
        $this->mimeType = $mimeType;
263
264
        return $this;
265
    }
266
    public function getOriginalName(): string
267
    {
268
        return $this->originalName;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->originalName could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
269
    }
270
    public function setOriginalName(?string $originalName): self
271
    {
272
        $this->originalName = $originalName;
273
274
        return $this;
275
    }
276
    public function getDimensions(): array
277
    {
278
        return $this->dimensions;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dimensions could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
279
    }
280
    public function setDimensions(?array $dimensions): self
281
    {
282
        $this->dimensions = $dimensions;
283
284
        return $this;
285
    }
286
    public function getWidth(): int
287
    {
288
        $data = $this->getDimensions();
289
        if ([] !== $data) {
290
            // $data = explode(',', $data);
291
            return (int) $data[0];
292
        }
293
294
        return 0;
295
    }
296
    public function getHeight(): int
297
    {
298
        $data = $this->getDimensions();
299
        if ([] !== $data) {
300
            // $data = explode(',', $data);
301
            return (int) $data[1];
302
        }
303
304
        return 0;
305
    }
306
    public function getMetadata(): array
307
    {
308
        return $this->metadata;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->metadata could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
309
    }
310
    public function setMetadata(array $metadata): self
311
    {
312
        $this->metadata = $metadata;
313
314
        return $this;
315
    }
316
    public function getDescription(): string
317
    {
318
        return $this->description;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->description could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
319
    }
320
    public function setDescription(string $description): self
321
    {
322
        $this->description = $description;
323
324
        return $this;
325
    }
326
    public function getFile(): ?File
327
    {
328
        return $this->file;
329
    }
330
331
    public function setFile(File|UploadedFile $file = null): self
332
    {
333
        $this->file = $file;
334
        if (null !== $file) {
335
            // It is required that at least one field changes if you are using doctrine
336
            // otherwise the event listeners won't be called and the file is lost
337
            $this->updatedAt = new DateTimeImmutable();
338
        }
339
340
        return $this;
341
    }
342
}
343