Passed
Push — master ( f972fc...fc48d8 )
by Gabriel
03:18
created

ConversionCollection::getByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
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
        $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 4
    public function forCollection(string $collectionName = '')
34
    {
35 4
        if ($collectionName === '') {
36
            return $this;
37
        }
38
39 4
        return $this->shouldBePerformedOn($collectionName);
40
    }
41
42
    /**
43
     * @param string $collectionName
44
     *
45
     * @return $this
46
     */
47 4
    protected function shouldBePerformedOn($collectionName)
48
    {
49 4
        $results = [];
50 4
        foreach ($this as $key => $item) {
51
            /** @var Conversion $item */
52 2
            if ($item->shouldBePerformedOn($collectionName)) {
53 2
                $results[$key] = $item;
54
            }
55
        }
56
57 4
        return new self($results);
58
    }
59
}
60