Completed
Push — master ( a81ea2...e23c5c )
by Gabriel
03:24
created

MediaRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 3
Bugs 1 Features 2
Metric Value
eloc 18
dl 0
loc 74
ccs 14
cts 18
cp 0.7778
rs 10
c 3
b 1
f 2
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultFilterFunction() 0 14 4
A applyFilterToCollection() 0 7 2
A getRecord() 0 3 1
A setRecord() 0 3 1
A getFilteredCollection() 0 3 1
1
<?php
2
3
namespace ByTIC\MediaLibrary\MediaRepository;
4
5
use ByTIC\MediaLibrary\Collections\Collection;
6
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait;
7
use ByTIC\MediaLibrary\Media\Media;
8
use Closure;
9
use Nip\Records\Record;
10
use Nip\Utility\Arr;
11
12
/**
13
 * Class MediaRepository.
14
 */
15
class MediaRepository
16
{
17
    use Traits\NewCollectionTrait;
18
19
    protected $loader;
20
21
    /**
22
     * @var \ByTIC\MediaLibrary\Collections\Collection[]
23
     */
24
    protected $collections;
25
26
    /**
27
     * @var Record|HasMediaTrait
28
     */
29
    protected $record;
30
31
    /**
32
     * @return Record|HasMediaTrait
33
     */
34 8
    public function getRecord(): Record
35
    {
36 8
        return $this->record;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->record could return the type ByTIC\MediaLibrary\HasMedia\HasMediaTrait which is incompatible with the type-hinted return Nip\Records\Record. Consider adding an additional type-check to rule them out.
Loading history...
37
    }
38
39
    /**
40
     * @param Record|HasMediaTrait $record
41
     */
42 9
    public function setRecord(Record $record)
43
    {
44 9
        $this->record = $record;
45 9
    }
46
47
    /**
48
     * @param string $collectionName
49
     * @param array  $filter
50
     *
51
     * @return Collection
52
     */
53 7
    public function getFilteredCollection(string $collectionName, $filter = []): Collection
54
    {
55 7
        return $this->applyFilterToCollection($this->getCollection($collectionName), $filter);
56
    }
57
58
    /**
59
     * Apply given filters on media.
60
     *
61
     * @param Collection     $collection
62
     * @param array|callable $filter
63
     *
64
     * @return Collection
65
     */
66 7
    protected function applyFilterToCollection(Collection $collection, $filter): Collection
67
    {
68 7
        if (is_array($filter)) {
69 7
            $filter = $this->getDefaultFilterFunction($filter);
70
        }
71
72 7
        return $collection->filter($filter);
73
    }
74
75 7
    protected function getDefaultFilterFunction(array $filters): Closure
76
    {
77
        return function (Media $media) use ($filters) {
78 5
            foreach ($filters as $property => $value) {
79
                if (! Arr::has($media->custom_properties, $property)) {
0 ignored issues
show
Bug introduced by
The property custom_properties does not seem to exist on ByTIC\MediaLibrary\Media\Media.
Loading history...
80
                    return false;
81
                }
82
83
                if (Arr::get($media->custom_properties, $property) !== $value) {
84
                    return false;
85
                }
86
            }
87
88 5
            return true;
89 7
        };
90
    }
91
}
92