Passed
Push — 0.6 ( fa70f4...7db85c )
by Philippe
06:36
created

SortAssets::handle()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Models\Application;
4
5
use Thinktomorrow\AssetLibrary\Models\Asset;
6
use Thinktomorrow\AssetLibrary\Interfaces\HasAsset;
7
use Thinktomorrow\AssetLibrary\Models\Application\AddAsset;
8
9
class SortAssets
10
{
11
    /**
12
     * Remove the asset and attaches a new one.
13
     *
14
     * @param $replace
15
     * @param $with
16
     * @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded
17
     * @throws \Thinktomorrow\AssetLibrary\Exceptions\AssetUploadException
18
     */
19 2
    public function handle(HasAsset $model, $type, $sorting)
20
    {
21 2
        $files = $model->assets($type);
0 ignored issues
show
Bug introduced by
The method assets() does not exist on Thinktomorrow\AssetLibrary\Interfaces\HasAsset. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\AssetLibrary\Interfaces\HasAsset. ( Ignorable by Annotation )

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

21
        /** @scrutinizer ignore-call */ 
22
        $files = $model->assets($type);
Loading history...
22
        $files->each(function ($asset) use ($model, $sorting) {
23 2
            if (in_array($asset->id, $sorting)) {
24 2
                $pivot = $model->assetRelation->find($asset->id)->pivot;
0 ignored issues
show
Bug introduced by
Accessing assetRelation on the interface Thinktomorrow\AssetLibrary\Interfaces\HasAsset suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
25 2
                $pivot->order = array_search($asset->id, $sorting);
26 2
                $pivot->save();
27
            }
28 2
        });
29 2
    }
30
}
31