Passed
Push — dependabot/composer/orchestra/... ( 137c82...0b1a28 )
by
unknown
04:18
created

DeleteAsset::deleteAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
     */
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 4
        $asset = Asset::find($id)->first();
36 4
        $media = $asset->media;
37 4
        foreach ($media as $file) {
38 4
            if (! is_file(public_path($file->getUrl())) || ! is_writable(public_path($file->getUrl()))) {
39 4
                throw new FileNotAccessibleException();
40
            }
41
        }
42 3
        $asset->delete();
43 3
    }
44
45
    /**
46
     * Removes all assets completely.
47
     *
48
     * @param $ids
49
     */
50 1
    public function deleteAll(HasAsset $model): void
51
    {
52 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...
53 1
    }
54
}
55