SortAssets::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 4
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Application;
4
5
use Thinktomorrow\AssetLibrary\HasAsset;
6
use Spatie\MediaLibrary\MediaCollections\Exceptions\FileCannotBeAdded;
7
8
class SortAssets
9
{
10
    /**
11
     * Remove the asset and attaches a new one.
12
     *
13
     * @param $replace
14
     * @param $with
15
     * @throws FileCannotBeAdded
16
     */
17 4
    public function handle(HasAsset $model, $sorting, $type, $locale)
18
    {
19 4
        $assets = $model->assetRelation()->where('asset_pivots.type', $type)->where('asset_pivots.locale', $locale)->get();
20
21 4
        foreach ($assets as $asset) {
22 4
            $pivot        = $asset->pivot;
23 4
            $pivot->order = array_search($asset->id, $sorting);
24
25 4
            $pivot->save();
26
        }
27 4
    }
28
}
29