Completed
Push — master ( 1fff09...65b094 )
by Terzi
05:17
created

MediaCollection::onIndex()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 0
1
<?php
2
3
namespace Terranet\Administrator\Field;
4
5
use Illuminate\Support\Facades\View;
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 Terranet\Administrator\Field\Traits\WorksWithModules;
8
use Terranet\Administrator\Scaffolding;
9
10
class MediaCollection extends Generic
11
{
12
    use WorksWithModules;
13
14
    protected $conversion = '';
15
16
    /**
17
     * @param string $conversion
18
     * @return self
19
     */
20
    public function convertedTo(string $conversion): self
21
    {
22
        $this->conversion = $conversion;
23
24
        return $this;
25
    }
26
27
    /**
28
     * @return \Illuminate\Contracts\View\View
29
     */
30
    protected function onIndex()
31
    {
32
        $media = $this->model->getMedia(
33
            $name = $this->id()
34
        );
35
        $module = $this->firstWithModel($this->model);
36
37
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('media' => ...app('scaffold.module')) returns the type array<string,Illuminate\...abase\Eloquent\Builder> which is incompatible with the documented return type Illuminate\Contracts\View\View.
Loading history...
38
            'media' => $media,
39
            'module' => $module ?: app('scaffold.module'),
40
        ];
41
    }
42
43
    /**
44
     * @return \Illuminate\Contracts\View\View
45
     */
46
    protected function onView()
47
    {
48
        $media = $this->model->getMedia(
49
            $name = $this->id()
50
        );
51
52
        $media = $media->map(function ($item) {
53
            return array_merge($item->toArray(), [
54
                'url' => $item->getUrl(),
55
                'conversions' => $this->conversions($item),
56
            ]);
57
        });
58
59
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('media' => ...' => $this->conversion) returns the type array<string,mixed|strin...abase\Eloquent\Builder> which is incompatible with the documented return type Illuminate\Contracts\View\View.
Loading history...
60
            'media' => $media->toJson(),
61
            'conversion' => $this->conversion,
62
        ];
63
    }
64
65
    /**
66
     * @return \Illuminate\Contracts\View\View
67
     */
68
    protected function onEdit()
69
    {
70
        $media = $this->model->getMedia(
71
            $name = $this->id()
72
        );
73
74
        $media = $media->map(function ($item) {
75
            return array_merge($item->toArray(), [
76
                'url' => $item->getUrl(),
77
                'conversions' => $this->conversions($item),
78
            ]);
79
        });
80
81
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('media' => ...' => $this->conversion) returns the type array<string,mixed|strin...abase\Eloquent\Builder> which is incompatible with the documented return type Illuminate\Contracts\View\View.
Loading history...
82
            'media' => $media->toJson(),
83
            'conversion' => $this->conversion,
84
        ];
85
    }
86
87
    /**
88
     * @param Media $item
89
     *
90
     * @return array
91
     */
92
    protected function conversions(Media $item)
93
    {
94
        return array_build($item->getMediaConversionNames(), function ($key, $conversion) use ($item) {
95
            return [$conversion, $item->getUrl($conversion)];
96
        });
97
    }
98
}
99