Test Failed
Push — 0.6 ( 7491e4...a828c1 )
by Philippe
05:42
created

DeleteAsset::remove()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 4
nop 1
dl 0
loc 13
ccs 0
cts 0
cp 0
crap 30
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Application;
4
5
use Thinktomorrow\AssetLibrary\Asset;
6
use Thinktomorrow\AssetLibrary\HasAsset;
7
use Thinktomorrow\AssetLibrary\Exceptions\FileNotAccessibleException;
8
9
class DeleteAsset
10
{
11
    /**
12
     * Removes an asset completely.
13
     *
14
     * @param $ids
15 1
     */
16
    public function delete($ids): void
17 1
    {
18 1
        if (is_array($ids)) {
19
            foreach ($ids as $id) {
20
                self::remove($id);
0 ignored issues
show
Bug Best Practice introduced by
The method Thinktomorrow\AssetLibra...n\DeleteAsset::remove() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
                self::/** @scrutinizer ignore-call */ 
21
                      remove($id);
Loading history...
21
            }
22
        } else {
23
            if (! $ids) {
24
                return;
25 1
            }
26
            self::remove($ids);
27 1
        }
28 1
    }
29
30
    public function remove($id)
31
    {
32
        if (! $id) {
33
            return false;
34
        }
35
        $asset = Asset::find($id)->first();
36
        $media = $asset->media;
37
        foreach ($media as $file) {
38
            if (! is_file(public_path($file->getUrl())) || ! is_writable(public_path($file->getUrl()))) {
39
                throw new FileNotAccessibleException();
40
            }
41
        }
42
        $asset->delete();
43
    }
44
45
    /**
46
     * Removes all assets completely.
47
     *
48
     * @param $ids
49
     */
50
    public function deleteAll(HasAsset $model): void
51
    {
52
        $model->assetRelation->each->delete();
0 ignored issues
show
Bug introduced by
Accessing assetRelation on the interface Thinktomorrow\AssetLibrary\HasAsset suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
53
    }
54
}
55