Completed
Push — master ( 9e9649...0ca17c )
by Freek
02:12
created

MediaRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\MediaLibrary;
4
5
use Illuminate\Support\Collection;
6
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia;
7
8
class MediaRepository
9
{
10
    /**
11
     * @var \Spatie\MediaLibrary\Media
12
     */
13
    protected $model;
14
15
    /**
16
     * @param \Spatie\MediaLibrary\Media $model
17
     */
18
    public function __construct(Media $model)
19
    {
20
        $this->model = $model;
21
    }
22
23
    /**
24
     * Get all media in the collection.
25
     *
26
     * @param HasMedia       $model
27
     * @param string         $collectionName
28
     * @param array|\Closure $filter
29
     *
30
     * @return Collection
31
     */
32
    public function getCollection(HasMedia $model, $collectionName, $filter = [])
33
    {
34
        $mediaCollection = $this->loadMedia($model, $collectionName);
35
36
        $mediaCollection = $this->applyFilterToMediaCollection($mediaCollection, $filter);
37
38
        return Collection::make($mediaCollection);
39
    }
40
41
    /**
42
     * Load media by collectionName.
43
     *
44
     * @param HasMedia $model
45
     * @param string   $collectionName
46
     *
47
     * @return mixed
48
     */
49
    protected function loadMedia(HasMedia $model, $collectionName)
50
    {
51
        if ($this->mediaIsPreloaded($model)) {
52
            $media = $model->media->filter(function (Media $mediaItem) use ($collectionName) {
53
54
                if ($collectionName == '') {
55
                    return true;
56
                }
57
58
                return $mediaItem->collection_name == $collectionName;
59
60
            })->sortBy(function (Media $media) {
61
62
                return $media->order_column;
63
64
            })->values();
65
66
            return $media;
67
        }
68
69
        $query = $model->media();
70
71
        if ($collectionName != '') {
72
            $query = $query->where('collection_name', $collectionName);
73
        }
74
75
        $media = $query
76
            ->orderBy('order_column')
77
            ->get();
78
79
        return $media;
80
    }
81
82
    /**
83
     * Determine if media is already preloaded on this model.
84
     *
85
     * @param HasMedia $model
86
     *
87
     * @return bool
88
     */
89
    protected function mediaIsPreloaded(HasMedia $model)
90
    {
91
        return isset($model->media);
92
    }
93
94
    /**
95
     * Apply given filters on media.
96
     *
97
     * @param \Illuminate\Support\Collection $media
98
     * @param array|\Closure                 $filter
99
     *
100
     * @return Collection
101
     */
102
    protected function applyFilterToMediaCollection(Collection $media, $filter)
103
    {
104
        if (is_array($filter)) {
105
            $filter = $this->getDefaultFilterFunction($filter);
106
        }
107
108
        return $media->filter($filter);
109
    }
110
111
    /**
112
     * Get all media.
113
     *
114
     * @return \Illuminate\Database\Eloquent\Collection
115
     */
116
    public function all()
117
    {
118
        return $this->model->all();
119
    }
120
121
    /**
122
     * Get all media for the given type.
123
     *
124
     * @param string $modelType
125
     *
126
     * @return \Illuminate\Database\Eloquent\Collection
127
     */
128
    public function getByModelType($modelType)
129
    {
130
        return $this->model->where('model_type', $modelType)->get();
131
    }
132
133
    /**
134
     * Get all media for the given type and collection name.
135
     *
136
     * @param string $modelType
137
     * @param string $collectionName
138
     *
139
     * @return \Illuminate\Database\Eloquent\Collection
140
     */
141
    public function getByModelTypeAndCollectionName($modelType, $collectionName)
142
    {
143
        return $this->model
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Spatie\MediaLibrary\Media>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
144
            ->where('model_type', $modelType)
145
            ->where('collection_name', $collectionName)
146
            ->get();
147
    }
148
149
    /**
150
     * Get all media for the given type and collection name.
151
     *
152
     * @param string $collectionName
153
     *
154
     * @return \Illuminate\Database\Eloquent\Collection
155
     */
156
    public function getByCollectionName($collectionName)
157
    {
158
        return $this->model
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Spatie\MediaLibrary\Media>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
159
            ->where('collection_name', $collectionName)
160
            ->get();
161
    }
162
163
    /**
164
     * Convert the given array to a filter function.
165
     *
166
     * @param $filters
167
     *
168
     * @return \Closure
169
     */
170
    protected function getDefaultFilterFunction($filters)
171
    {
172
        return function (Media $media) use ($filters) {
173
174
            $customProperties = $media->custom_properties;
175
176
            foreach ($filters as $property => $value) {
177
                if (!isset($customProperties[$property])) {
178
                    return false;
179
                }
180
                if ($customProperties[$property] != $value) {
181
                    return false;
182
                }
183
            }
184
185
            return true;
186
        };
187
    }
188
}
189