Completed
Push — master ( ad6b4f...e57368 )
by Terzi
05:01 queued 10s
created

MediaLibraryProvider::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terranet\Administrator\Services;
4
5
use Spatie\MediaLibrary\HasMedia\HasMedia;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\HasMedia\HasMedia was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Spatie\MediaLibrary\Models\Media;
0 ignored issues
show
Bug introduced by
The type Spatie\MediaLibrary\Models\Media was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
}