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

ConversionCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 47
ccs 14
cts 16
cp 0.875
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A forCollection() 0 7 2
A getByName() 0 11 2
A shouldBePerformedOn() 0 11 3
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