ImageCollection::findImageByUUID()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Media;
4
5
use ArrayIterator;
6
use TwoDotsTwice\Collection\AbstractCollection;
7
use TwoDotsTwice\Collection\CollectionInterface;
8
use ValueObjects\Identity\UUID;
9
10
class ImageCollection extends AbstractCollection implements CollectionInterface
11
{
12
    /**
13
     * @var Image|null
14
     */
15
    protected $mainImage;
16
17
    protected function getValidObjectType()
18
    {
19
        return Image::class;
20
    }
21
22
    /**
23
     * @param Image $image
24
     * @return ImageCollection
25
     */
26
    public function withMain(Image $image)
27
    {
28
        $collection = $this->contains($image) ? $this : $this->with($image);
29
30
        $copy = clone $collection;
31
        $copy->mainImage = $image;
0 ignored issues
show
Bug introduced by
The property mainImage does not seem to exist in TwoDotsTwice\Collection\AbstractCollection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
32
        return $copy;
33
    }
34
35
    /**
36
     * @return Image|null
37
     */
38
    public function getMain()
39
    {
40
        if (0 === $this->length()) {
41
            return null;
42
        }
43
44
        if ($this->mainImage) {
45
            return $this->mainImage;
46
        } else {
47
            /** @var ArrayIterator $iterator */
48
            $iterator = $this->getIterator();
49
            $iterator->rewind();
50
51
            return $iterator->current();
52
        }
53
    }
54
55
    /**
56
     * @param UUID $uuid
57
     * @return Image|null
58
     */
59
    public function findImageByUUID(UUID $uuid)
60
    {
61
        /** @var Image $image */
62
        foreach ($this->items as $image) {
63
            if ($image->getMediaObjectId()->sameValueAs($uuid)) {
64
                return $image;
65
            }
66
        }
67
68
        return null;
69
    }
70
}
71