Completed
Push — master ( e4d2fe...b5dced )
by
unknown
17s
created

ImageCollection::findImageByUUID()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Media;
4
5
use TwoDotsTwice\Collection\AbstractCollection;
6
use TwoDotsTwice\Collection\CollectionInterface;
7
use ValueObjects\Identity\UUID;
8
9
class ImageCollection extends AbstractCollection implements CollectionInterface
10
{
11
    /**
12
     * @var Image|null
13
     */
14
    protected $mainImage;
15
16
    protected function getValidObjectType()
17
    {
18
        return Image::class;
19
    }
20
21
    /**
22
     * @param Image $image
23
     * @return ImageCollection
24
     */
25
    public function withMain(Image $image)
26
    {
27
        $collection = $this->contains($image) ? $this : $this->with($image);
28
29
        $copy = clone $collection;
30
        $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...
31
        return $copy;
32
    }
33
34
    /**
35
     * @return Image|null
36
     */
37
    public function getMain()
38
    {
39
        if (0 === $this->length()) {
40
            return null;
41
        }
42
43
        if ($this->mainImage) {
44
            return $this->mainImage;
45
        } else {
46
            $iterator = $this->getIterator();
47
            $iterator->rewind();
48
49
            return $iterator->current();
50
        }
51
    }
52
53
    /**
54
     * @param UUID $uuid
55
     * @return Image|null
56
     */
57
    public function findImageByUUID(UUID $uuid)
58
    {
59
        /** @var Image $image */
60
        foreach ($this->items as $image) {
61
            if ($image->getMediaObjectId() === $uuid) {
62
                return $image;
63
            }
64
        }
65
        
66
        return null;
67
    }
68
}
69