Passed
Push — master ( 1d74eb...84916e )
by Philippe
04:36
created

DeleteAsset::remove()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.0359

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 9
c 1
b 0
f 1
nc 5
nop 1
dl 0
loc 18
ccs 9
cts 10
cp 0.9
crap 6.0359
rs 9.2222
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Application;
4
5
use Thinktomorrow\AssetLibrary\Asset;
6
use Thinktomorrow\AssetLibrary\Exceptions\FileNotAccessibleException;
7
use Thinktomorrow\AssetLibrary\HasAsset;
8
9
class DeleteAsset
10
{
11
    /**
12
     * Removes an asset completely.
13
     *
14
     * @param $ids
15
     */
16 5
    public function delete($ids): void
17
    {
18 5
        if (is_array($ids)) {
19 2
            foreach ($ids as $id) {
20 2
                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 4
            if (! $ids) {
24 1
                return;
25
            }
26 3
            self::remove($ids);
27
        }
28 4
    }
29
30 5
    public function remove($id)
31
    {
32 5
        if (! $id) {
33 1
            return false;
34
        }
35
36 4
        if (! $asset = Asset::find($id)) {
37
            return false;
38
        }
39
40 4
        $media = $asset->media;
41 4
        foreach ($media as $file) {
42 4
            if (! is_file(public_path($file->getUrl())) || ! is_writable(public_path($file->getUrl()))) {
43 4
                throw new FileNotAccessibleException();
44
            }
45
        }
46
47 3
        $asset->delete();
48 3
    }
49
50
    /**
51
     * Removes all assets completely.
52
     *
53
     * @param $ids
54
     */
55 1
    public function deleteAll(HasAsset $model): void
56
    {
57 1
        $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...
58 1
    }
59
}
60