Completed
Push — master ( 4fda4b...027cf3 )
by Matt
05:35
created

Image::getSizeInBytes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Entity;
4
5
use ApiPlatform\Core\Annotation\ApiFilter;
6
use ApiPlatform\Core\Annotation\ApiResource;
7
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
8
use App\EventListener\ImageCalculatedFieldSetterListener;
9
use App\EventListener\WanderUploadListener;
10
use App\Repository\ImageRepository;
11
use Doctrine\Common\Collections\ArrayCollection;
12
use Doctrine\Common\Collections\Collection;
13
use Doctrine\ORM\Mapping as ORM;
14
use Symfony\Component\HttpFoundation\File\File;
15
use Vich\UploaderBundle\Mapping\Annotation as Vich;
16
use Symfony\Component\Validator\Constraints as Assert;
17
use Symfony\Component\Serializer\Annotation\Groups;
18
use Symfony\Component\Serializer\Annotation\Ignore;
19
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
20
use Doctrine\ORM\Event\LifecycleEventArgs;
21
22
/**
23
 * @ApiResource(
24
 *  collectionOperations={"get"={"normalization_context"={"groups"="image:list"}}},
25
 *  itemOperations={"get"={"normalization_context"={"groups"="image:item"}}},
26
 *  order={"capturedAt"="ASC"},
27
 *  paginationEnabled=false
28
 * )
29
 *
30
 * @ApiFilter(SearchFilter::class, properties={"wanders": "exact"})
31
 * @ApiFilter(ExistsFilter::class, properties={"latlng"})
32
 *
33
 * @ORM\Entity(repositoryClass=ImageRepository::class)
34
 *
35
 * @ORM\EntityListeners({
36
 *     ImageCalculatedFieldSetterListener::class
37
 * })
38
 *
39
 * @ORM\HasLifecycleCallbacks()
40
 *
41
 * This is just to control the stuff that goes back from our one controller
42
 * action that returns a JSON response, ImageController::upload
43
 *
44
 * @Vich\Uploadable
45
 */
46
47
class Image
48
{
49
    /**
50
     * @ORM\Id
51
     * @ORM\GeneratedValue
52
     * @ORM\Column(type="integer")
53
     *
54
     * @Groups({"image:list", "image:item"})
55
     */
56
    private $id;
57
58
    // TODO: We probably don't want this massive field being returned
59
    // as part of any API response, etc.
60
    /**
61
     * @Vich\UploadableField(mapping="image", fileNameProperty="name", size="sizeInBytes",
62
     *  mimeType="mimeType", originalName="originalName", dimensions="dimensions")
63
     *
64
     * @Ignore()
65
     */
66
    private $imageFile;
67
68
    /**
69
     * @ORM\Column(type="string", length=255, nullable=true)
70
     *
71
     * @Groups({"image:list", "image:item"})
72
     */
73
    private $name; // For Vich, not for us. We use Title.
74
75
    /**
76
     * @ORM\Column(type="string", length=255, nullable=true)
77
     *
78
     * @Groups({"image:list", "image:item"})
79
     */
80
    private $title;
81
82
    /**
83
     * @ORM\Column(type="text", nullable=true)
84
     *
85
     * @Groups({"image:list", "image:item"})
86
     */
87
    private $description;
88
89
    /**
90
     * @ORM\Column(type="integer", nullable=true)
91
     *
92
     * @Groups({"image:list", "image:item"})
93
     */
94
    private $sizeInBytes;
95
96
    /**
97
     * @ORM\Column(type="string", length=255, nullable=true)
98
     *
99
     * @Groups({"image:list", "image:item"})
100
     */
101
    private $mimeType;
102
103
    /**
104
     * @ORM\Column(type="string", length=255, nullable=true)
105
     *
106
     * @Groups({"image:list", "image:item"})
107
     */
108
    private $originalName;
109
110
    /**
111
     * @ORM\Column(type="simple_array", nullable=true)
112
     *
113
     * @Groups({"image:list", "image:item"})
114
     */
115
    private $dimensions = [];
116
117
    /**
118
     * @ORM\Column(type="datetime")
119
     *
120
     * @Groups({"image:list", "image:item"})
121
     *
122
     * @var \DateTimeInterface|null
123
     */
124
    private $updatedAt;
125
126
    /**
127
     * @ORM\Column(type="simple_array", nullable=true)
128
     * @Assert\Count(
129
     *      min = 2,
130
     *      max = 2,
131
     *      minMessage = "There must be exactly two numbers in a latitude/longitude pair",
132
     *      maxMessage = "There must be exactly two numbers in a latitude/longitude pair",
133
     *      exactMessage = "Co-ordinates must consist of a latitude, longitude pair."
134
     * )
135
     *
136
     * @Groups({"image:list", "image:item"})
137
     *
138
     */
139
    private $latlng = [];
140
141
    /**
142
     * @ORM\Column(type="array", nullable=true, )
143
     *
144
     * @Groups({"image:list", "image:item"})
145
     */
146
    private $keywords = [];
147
148
    /**
149
     * @ORM\Column(type="datetime", nullable=true)
150
     *
151
     * @Groups({"image:list", "image:item"})
152
     */
153
    private $capturedAt;
154
155
    /**
156
     * @ORM\Column(type="integer", nullable=true)
157
     * @Groups({"image:list", "image:item"})
158
     */
159
    private $rating;
160
161
162
    // TODO: This @Ignore was here from when this was a many-to-many. Do we still
163
    // need it?
164
    /**
165
     * @ORM\ManyToOne(targetEntity=Wander::class, inversedBy="images")
166
     *
167
     * @Ignore()
168
     */
169
    private $wander;
170
171
    public function __construct()
172
    {
173
        // $this->wanders = new ArrayCollection();
174
    }
175
176
    /**
177
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
178
     * of 'UploadedFile' is injected into this setter to trigger the update. If this
179
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
180
     * must be able to accept an instance of 'File' as the bundle will inject one here
181
     * during Doctrine hydration.
182
     *
183
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
184
     */
185
    public function setImageFile(?File $imageFile = null): void
186
    {
187
        $this->imageFile = $imageFile;
188
189
        if (null !== $imageFile) {
190
            // It is required that at least one field changes if you are using doctrine
191
            // otherwise the event listeners won't be called and the file is lost
192
            $this->updatedAt = new \DateTimeImmutable();
193
        }
194
    }
195
196
    /**
197
     * @Ignore()
198
     */
199
    public function getImageFile(): ?File
200
    {
201
        return $this->imageFile;
202
    }
203
204
    public function getId(): ?int
205
    {
206
        return $this->id;
207
    }
208
209
    public function getName(): ?string
210
    {
211
        return $this->name;
212
    }
213
214
    public function setName(?string $name): self
215
    {
216
        $this->name = $name;
217
218
        return $this;
219
    }
220
221
    public function getTitle(): ?string
222
    {
223
        return $this->title;
224
    }
225
226
    public function setTitle(?string $title): self
227
    {
228
        $this->title = $title;
229
230
        return $this;
231
    }
232
233
    public function getDescription(): ?string
234
    {
235
        return $this->description;
236
    }
237
238
    public function setDescription(?string $description): self
239
    {
240
        $this->description = $description;
241
242
        return $this;
243
    }
244
245
    public function getSizeInBytes(): ?int
246
    {
247
        return $this->sizeInBytes;
248
    }
249
250
    public function setSizeInBytes(?int $sizeInBytes): self
251
    {
252
        $this->sizeInBytes = $sizeInBytes;
253
254
        return $this;
255
    }
256
257
    public function getMimeType(): ?string
258
    {
259
        return $this->mimeType;
260
    }
261
262
    public function setMimeType(?string $mimeType): self
263
    {
264
        $this->mimeType = $mimeType;
265
266
        return $this;
267
    }
268
269
    public function getOriginalName(): ?string
270
    {
271
        return $this->originalName;
272
    }
273
274
    public function setOriginalName(?string $originalName): self
275
    {
276
        $this->originalName = $originalName;
277
278
        return $this;
279
    }
280
281
    public function getDimensions(): ?array
282
    {
283
        return $this->dimensions;
284
    }
285
286
    public function setDimensions(?array $dimensions): self
287
    {
288
        $this->dimensions = $dimensions;
289
290
        return $this;
291
    }
292
293
    public function getUpdatedAt(): ?\DateTimeInterface
294
    {
295
        return $this->updatedAt;
296
    }
297
298
    public function setUpdatedAt(\DateTimeInterface $updatedAt): self
299
    {
300
        $this->updatedAt = $updatedAt;
301
        return $this;
302
    }
303
304
    public function getLatitude(): ?float
305
    {
306
        if ($this->latlng === null ||
307
            !is_array($this->latlng) ||
308
            empty($this->latlng)) {
309
            return null;
310
        }
311
        return $this->latlng[0];
312
    }
313
314
    public function getLongitude(): ?float
315
    {
316
        if ($this->latlng === null ||
317
            !is_array($this->latlng) ||
318
            empty($this->latlng)) {
319
            return null;
320
        }
321
        return $this->latlng[1];
322
    }
323
324
    public function getLatlng(): ?array
325
    {
326
        return $this->latlng;
327
    }
328
329
    public function hasLatlng(): bool
330
    {
331
        return is_array($this->latlng) && count($this->latlng) == 2;
332
    }
333
334
    public function setLatlng(?array $latlng): self
335
    {
336
        $this->latlng = $latlng;
337
338
        return $this;
339
    }
340
341
    public function getKeywords(): ?array
342
    {
343
        return $this->keywords;
344
    }
345
346
    public function setKeywords(?array $keywords): self
347
    {
348
        $this->keywords = $keywords;
349
350
        return $this;
351
    }
352
353
    public function getCapturedAt(): ?\DateTimeInterface
354
    {
355
        return $this->capturedAt;
356
    }
357
358
    public function setCapturedAt(\DateTimeInterface $capturedAt): self
359
    {
360
        $this->capturedAt = $capturedAt;
361
362
        return $this;
363
    }
364
365
    // /**
366
    //  * @return Collection|Wander[]
367
    //  */
368
    // public function getWanders(): Collection
369
    // {
370
    //     return $this->wanders;
371
    // }
372
373
    // public function addWander(Wander $wander): self
374
    // {
375
    //     if (!$this->wanders->contains($wander)) {
376
    //         $this->wanders[] = $wander;
377
    //         $wander->addImage($this);
378
    //     }
379
380
    //     return $this;
381
    // }
382
383
    // public function removeWander(Wander $wander): self
384
    // {
385
    //     if ($this->wanders->removeElement($wander)) {
386
    //         $wander->removeImage($this);
387
    //     }
388
389
    //     return $this;
390
    // }
391
392
    public function getWander(): ?Wander
393
    {
394
        return $this->wander;
395
    }
396
397
    public function setWander(?Wander $wander): self
398
    {
399
        $this->wander = $wander;
400
        return $this;
401
    }
402
403
    public function getRating(): ?int
404
    {
405
        return $this->rating;
406
    }
407
408
    public function setRating(?int $rating): self
409
    {
410
        $this->rating = $rating;
411
412
        return $this;
413
    }
414
415
416
    /* Computed (set up by Doctrine postLoad listener) */
417
418
    /**
419
     * @Groups({"image:list", "image:item"})
420
     */
421
    private $imageUri;
422
423
    /**
424
     * @Groups({"image:list", "image:item"})
425
     */
426
    private $markerImageUri;
427
428
    /**
429
     * @Groups({"image:list", "image:item"})
430
     */
431
    private $mediumImageUri;
432
433
    /**
434
     * @Groups({"image:list", "image:item"})
435
     */
436
    private $imageShowUri;
437
438
    /**
439
     * @ORM\Column(type="array", nullable=true)
440
     */
441
    private $auto_tags = [];
442
443
    /**
444
     * @ORM\Column(type="string", length=255, nullable=true)
445
     */
446
    private $location;
447
448
449
    public function setImageUri($imageUri) {
450
        $this->imageUri = $imageUri;
451
    }
452
453
    public function getImageUri(): ?string {
454
        return $this->imageUri;
455
    }
456
457
    public function setMarkerImageUri($markerImageUri) {
458
        $this->markerImageUri = $markerImageUri;
459
    }
460
    public function getMarkerImageUri(): ?string {
461
        return $this->markerImageUri;
462
    }
463
464
    public function setMediumImageUri($mediumImageUri) {
465
        $this->mediumImageUri = $mediumImageUri;
466
    }
467
    public function getMediumImageUri(): ?string {
468
        return $this->mediumImageUri;
469
    }
470
471
    public function setImageShowUri($imageShowUri) {
472
        $this->imageShowUri = $imageShowUri;
473
    }
474
    public function getImageShowUri(): ?string {
475
        return $this->imageShowUri;
476
    }
477
478
    /**
479
     * @ORM\PostUpdate
480
     * @ORM\PostPersist
481
     */
482
    public function updateWanderToForceElasticReindex(LifecycleEventArgs $args): self
483
    {
484
        // TODO: We may need extra code or a different function
485
        // if we're to cover the event of an image being deleted,
486
        // maybe. Test. But for now this is good enough and we can
487
        // re-index manually if need be.
488
        $wander = $this->getWander();
489
        if ($wander === null) {
490
            return $this;
491
        }
492
        $wander->setModifiedAt();
493
        $entityManager = $args->getEntityManager();
494
        $entityManager->persist($wander);
495
        $entityManager->flush();
496
        return $this;
497
    }
498
499
    public function getAutoTags(): ?array
500
    {
501
        return $this->auto_tags;
502
    }
503
504
    public function setAutoTags(?array $auto_tags): self
505
    {
506
        $this->auto_tags = $auto_tags;
507
508
        return $this;
509
    }
510
    public function getAutoTagsCount(): int {
511
        if (is_array($this->auto_tags)) {
0 ignored issues
show
introduced by
The condition is_array($this->auto_tags) is always true.
Loading history...
512
            return count($this->auto_tags);
513
        }
514
        return 0;
515
    }
516
517
    public function getLocation(): ?string
518
    {
519
        return $this->location;
520
    }
521
522
    public function hasLocation(): bool
523
    {
524
        return $this->location !== null && $this->location <> '';
525
    }
526
527
    public function setLocation(?string $location): self
528
    {
529
        $this->location = $location;
530
531
        return $this;
532
    }
533
}
534
535