1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Services; |
4
|
|
|
|
5
|
|
|
use Spatie\MediaLibrary\HasMedia\HasMedia; |
|
|
|
|
6
|
|
|
use Spatie\MediaLibrary\Models\Media; |
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
8
|
|
|
|
9
|
|
|
class MediaLibraryProvider |
10
|
|
|
{ |
11
|
|
|
const COLLECTION_DEFAULT = 'default'; |
12
|
|
|
|
13
|
|
|
/** @var HasMedia */ |
14
|
|
|
protected $model; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* MediaLibraryProvider constructor. |
18
|
|
|
* @param HasMedia $model |
19
|
|
|
*/ |
20
|
|
|
protected function __construct(HasMedia $model) |
21
|
|
|
{ |
22
|
|
|
$this->model = $model; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param HasMedia $model |
27
|
|
|
* @return MediaLibraryProvider |
28
|
|
|
*/ |
29
|
|
|
public static function forModel(HasMedia $model): MediaLibraryProvider |
30
|
|
|
{ |
31
|
|
|
return new static($model); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Calculate items count per collection. |
36
|
|
|
* |
37
|
|
|
* @param string $collection |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function count(string $collection = self::COLLECTION_DEFAULT) |
41
|
|
|
{ |
42
|
|
|
return $this->model->media()->where('collection_name', $collection)->count(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Fetch all media in collection. |
47
|
|
|
* |
48
|
|
|
* @param string $collection |
49
|
|
|
* @param int $perPage |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function fetch(string $collection = self::COLLECTION_DEFAULT, $perPage = 20) |
53
|
|
|
{ |
54
|
|
|
return $this->model->media() |
55
|
|
|
->where('collection_name', $collection) |
56
|
|
|
->orderBy('created_at', 'desc') |
57
|
|
|
->paginate($perPage); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param UploadedFile $file |
62
|
|
|
* @param string $collection |
63
|
|
|
* @return \Spatie\MediaLibrary\Models\Media |
64
|
|
|
*/ |
65
|
|
|
public function attach(UploadedFile $file, string $collection): Media |
66
|
|
|
{ |
67
|
|
|
return $this->model->addMedia($file)->toMediaCollection($collection); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $mediaId |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function detach($mediaId) |
75
|
|
|
{ |
76
|
|
|
return $this->model->deleteMedia($mediaId); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Provide extra information. |
81
|
|
|
* |
82
|
|
|
* @param Media $item |
83
|
|
|
* @return Media |
84
|
|
|
*/ |
85
|
|
|
public static function toJson(Media $item) |
86
|
|
|
{ |
87
|
|
|
$item->url = $item->getUrl(); |
88
|
|
|
$item->conversions = array_build( |
89
|
|
|
$item->getMediaConversionNames(), |
90
|
|
|
function ($key, $conversion) use ($item) { |
91
|
|
|
return [$conversion, $item->getUrl($conversion)]; |
92
|
|
|
} |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
return $item; |
96
|
|
|
} |
97
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths