Passed
Branch feature/cropping (acc0fc)
by Philippe
12:01
created

AssetTrait::replace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Traits;
4
5
use Illuminate\Support\Collection;
6
use Thinktomorrow\AssetLibrary\Models\Asset;
7
use Thinktomorrow\AssetLibrary\Models\AssetUploader;
8
use Thinktomorrow\Locale\Locale;
9
10
trait AssetTrait
11
{
12
    /**
13
     * @return mixed
14
     */
15 21
    public function assets()
16
    {
17 21
        return $this->morphToMany(Asset::class, 'entity', 'asset_pivots')->withPivot('type', 'locale');
0 ignored issues
show
Bug introduced by
It seems like morphToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
18
    }
19
20
    /**
21
     * @param string $type
22
     * @param string|null $locale
23
     * @return bool
24
     */
25 4
    public function hasFile($type = '', $locale = null): bool
26
    {
27 4
        $filename = $this->getFilename($type, $locale);
28
29 4
        return (bool) $filename && basename($filename) != 'other.png';
30
    }
31
32
    /**
33
     * @param string $type
34
     * @param string|null $locale
35
     * @return string
36
     */
37 6
    public function getFilename($type = '', $locale = null): string
38
    {
39 6
        return basename($this->getFileUrl($type, '', $locale));
40
    }
41
42
    /**
43
     * @param string $type
44
     * @param string $size
45
     * @param string|null $locale
46
     * @return string
47
     */
48 17
    public function getFileUrl($type = '', $size = '', $locale = null): ?string
49
    {
50 17
        if ($this->assets->first() === null || $this->assets->first()->pivot === null) {
0 ignored issues
show
Bug introduced by
The property assets does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51 2
            return null;
52
        }
53
54 17
        $locale = $this->normalizeLocale($locale);
55
56 17
        $assets = $this->assets->where('pivot.type', $type);
57 17
        if ($assets->count() > 1) {
58 5
            $assets = $assets->where('pivot.locale', $locale);
59
        }
60
61 17
        if ($assets->isEmpty()) {
62 1
            return null;
63
        }
64
65 17
        return $assets->first()->getFileUrl($size);
66
    }
67
68
    /**
69
     * Adds a file to this model, accepts a type and locale to be saved with the file.
70
     *
71
     * @param $file
72
     * @param $type
73
     * @param string|null $locale
74
     */
75 8
    public function addFile($file, $type = '', $locale = null): void
76
    {
77 8
        $locale = $this->normalizeLocale($locale);
78
79 8
        $asset = AssetUploader::upload($file);
80 8
        if($asset instanceof Asset){
81 8
            $asset->attachToModel($this, $type, $locale);
82
        }
83 8
    }
84
85
    /**
86
     * Adds multiple files to this model, accepts a type and locale to be saved with the file.
87
     *
88
     * @param $files
89
     * @param $type
90
     * @param string|null $locale
91
     */
92 3
    public function addFiles($files, $type = '', $locale = null): void
93
    {
94 3
        $files = (array) $files;
95 3
        $locale = $this->normalizeLocale($locale);
96
97 3
        $asset = AssetUploader::upload($files);
98
99 3
        collect($asset)->each->attachToModel($this, $type, $locale);
100 3
    }
101
102
    /**
103
     * @return mixed
104
     */
105
    public function getAllImages()
106
    {
107 1
        $images = $this->assets->filter(function ($asset) {
108 1
            return $asset->getExtensionForFilter() === 'image';
109 1
        });
110
111 1
        return $images;
112
    }
113
114
    /**
115
     * Removes an asset completely.
116
     *
117
     * @param $ids
118
     */
119 1
    public function deleteAsset($ids): void
120
    {
121 1
        Asset::remove($ids);
122 1
    }
123
124
    /**
125
     * Remove the asset and attaches a new one.
126
     *
127
     * @param $replace
128
     * @param $with
129
     */
130 1
    public function replace($replace, $with): void
131
    {
132 1
        $asset = Asset::findOrFail($replace);
133 1
        Asset::remove($replace);
134
135 1
        $this->addFile(Asset::findOrFail($with), $asset->type, $asset->locale);
136 1
    }
137
138
    /**
139
     * @param null $type
140
     * @param string|null $locale
141
     * @return mixed
142
     */
143 4
    public function getAllFiles($type = null, $locale = null)
144
    {
145 4
        $locale = $this->normalizeLocale($locale);
146
147 4
        $files = $this->assets->where('pivot.type', $type)->where('pivot.locale', $locale);
148
149 4
        return $files;
150
    }
151
152
    /**
153
     * @param string|null $locale
154
     * @return string
155
     */
156 20
    private function normalizeLocale($locale = null): string
157
    {
158 20
        $locale = $locale ?? Locale::getDefault();
159
160 20
        return $locale;
161
    }
162
}
163