Passed
Push — master ( 9aef17...9e5266 )
by Gabriel
05:26
created

ConversionCollection::getByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 2.0116
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\Conversions;
4
5
use ByTIC\MediaLibrary\Exceptions\InvalidConversion;
6
use Nip\Collections\AbstractCollection;
7
8
/**
9
 * Class ConversionCollection.
10
 */
11
class ConversionCollection extends AbstractCollection
12
{
13 1
    public function getByName(string $name): Conversion
14
    {
15 1
        $conversion = $this->first(function (Conversion $conversion) use ($name) {
16 1
            return $conversion->getName() === $name;
17 1
        });
18
19 1
        if (!$conversion) {
20
            throw InvalidConversion::unknownName($name);
21
        }
22
23 1
        return $conversion;
24
    }
25
26
    /**
27
     * Get all the conversions in the collection.
28
     *
29
     * @param string $collectionName
30
     *
31
     * @return $this
32
     */
33 5
    public function forCollection(string $collectionName = '')
34
    {
35 5
        if ($collectionName === '') {
36
            return $this;
37
        }
38
39 5
        return $this->shouldBePerformedOn($collectionName);
40
    }
41
42
    /**
43
     * @param string $collectionName
44
     *
45
     * @return $this
46
     */
47 5
    protected function shouldBePerformedOn($collectionName)
48
    {
49 5
        $results = [];
50 5
        foreach ($this as $key => $item) {
51
            /** @var Conversion $item */
52 3
            if ($item->shouldBePerformedOn($collectionName)) {
53 3
                $results[$key] = $item;
54
            }
55
        }
56
57 5
        return new self($results);
58
    }
59
}
60