Completed
Push — master ( b25e0f...6cb20f )
by Terzi
06:11
created

Media::conversions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Terranet\Administrator\Field;
4
5
use Illuminate\Support\Facades\View;
6
use Spatie\MediaLibrary\Models\Media as MediaModel;
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 Media 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($this->id());
33
        $module = $this->firstWithModel($this->model) ?: app('scaffold.module');
34
35
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('media' => ...a, 'module' => $module) returns the type array<string,Illuminate\...abase\Eloquent\Builder> which is incompatible with the documented return type Illuminate\Contracts\View\View.
Loading history...
36
            'media' => $media,
37
            'module' => $module,
38
        ];
39
    }
40
41
    /**
42
     * @return \Illuminate\Contracts\View\View
43
     */
44
    protected function onView()
45
    {
46
        $media = $this->model->getMedia($this->id());
47
        $media = $media->map(function ($item) {
48
            return array_merge($item->toArray(), [
49
                'url' => $item->getUrl(),
50
                'conversions' => $this->conversions($item),
51
            ]);
52
        });
53
54
        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...
55
            'media' => $media,
56
            'conversion' => $this->conversion,
57
        ];
58
    }
59
60
    /**
61
     * @return \Illuminate\Contracts\View\View
62
     */
63
    protected function onEdit()
64
    {
65
        return $this->onView();
66
    }
67
68
    /**
69
     * @param MediaModel $item
70
     *
71
     * @return array
72
     */
73
    protected function conversions(MediaModel $item)
74
    {
75
        return array_build($item->getMediaConversionNames(), function ($key, $conversion) use ($item) {
76
            return [$conversion, $item->getUrl($conversion)];
77
        });
78
    }
79
}
80