Completed
Push — master ( 090ba5...2dbd35 )
by Freek
01:55
created

MediaRepository::loadMedia()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 3
nop 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A MediaRepository::all() 0 4 1
A MediaRepository::getByModelType() 0 4 1
1
<?php
2
3
namespace Spatie\MediaLibrary;
4
5
use Illuminate\Support\Collection;
6
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia;
7
use Illuminate\Database\Eloquent\Collection as DbCollection;
8
9
class MediaRepository
10
{
11
    /**
12
     * @var \Spatie\MediaLibrary\Media
13
     */
14
    protected $model;
15
16
    /**
17
     * @param \Spatie\MediaLibrary\Media $model
18
     */
19
    public function __construct(Media $model)
20
    {
21
        $this->model = $model;
22
    }
23
24
    /**
25
     * Get all media in the collection.
26
     *
27
     * @param HasMedia       $model
28
     * @param string         $collectionName
29
     * @param array|callable $filter
30
     *
31
     * @return Collection
32
     */
33
    public function getCollection(HasMedia $model, string $collectionName, $filter = []) : Collection
34
    {
35
        $mediaCollection = $model->loadMedia($collectionName);
36
37
        $mediaCollection = $this->applyFilterToMediaCollection($mediaCollection, $filter);
38
39
        return collect($mediaCollection);
40
    }
41
42
    /**
43
     * Apply given filters on media.
44
     *
45
     * @param \Illuminate\Support\Collection $media
46
     * @param array|callable                 $filter
47
     *
48
     * @return Collection
49
     */
50
    protected function applyFilterToMediaCollection(Collection $media, $filter) : Collection
51
    {
52
        if (is_array($filter)) {
53
            $filter = $this->getDefaultFilterFunction($filter);
54
        }
55
56
        return $media->filter($filter);
57
    }
58
59
    /**
60
     * Get all media.
61
     */
62
    public function all() : DbCollection
63
    {
64
        return $this->model->all();
65
    }
66
67
    /*
68
     * Get all media for the given type.
69
     */
70
    public function getByModelType(string $modelType) : DbCollection
71
    {
72
        return $this->model->where('model_type', $modelType)->get();
73
    }
74
75
    /*
76
     * Get media by ids.
77
     */
78
    public function getByIds(array $ids) : DbCollection
79
    {
80
        return $this->model->whereIn('id', $ids)->get();
81
    }
82
83
    /*
84
     * Get all media for the given type and collection name.
85
     */
86
    public function getByModelTypeAndCollectionName(string $modelType, string $collectionName) : DbCollection
87
    {
88
        return $this->model
89
            ->where('model_type', $modelType)
90
            ->where('collection_name', $collectionName)
91
            ->get();
92
    }
93
94
    /*
95
     * Get all media for the given type and collection name.
96
     */
97
    public function getByCollectionName(string $collectionName) : DbCollection
98
    {
99
        return $this->model
100
            ->where('collection_name', $collectionName)
101
            ->get();
102
    }
103
104
    /**
105
     * Convert the given array to a filter function.
106
     *
107
     * @param $filters
108
     *
109
     * @return \Closure
110
     */
111
    protected function getDefaultFilterFunction(array $filters)
112
    {
113
        return function (Media $media) use ($filters) {
114
            $customProperties = $media->custom_properties;
115
116
            foreach ($filters as $property => $value) {
117
                if (! isset($customProperties[$property])) {
118
                    return false;
119
                }
120
                if ($customProperties[$property] != $value) {
121
                    return false;
122
                }
123
            }
124
125
            return true;
126
        };
127
    }
128
}
129