Test Setup Failed
Push — 0.9 ( a3ca13...705e7e )
by Ben
03:57
created

ReorderAssets   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 50
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A batchUpdateColumn() 0 22 4
A handle() 0 16 1
1
<?php
2
3
namespace Thinktomorrow\AssetLibrary\Application;
4
5
use Illuminate\Support\Facades\DB;
6
use Thinktomorrow\AssetLibrary\Asset;
7
use Thinktomorrow\AssetLibrary\HasAsset;
8
9
class ReorderAssets
10
{
11
    public function handle(HasAsset $model, string $type, string $locale, array $orderedAssetIds): void
12
    {
13
        $model->assetRelation()
14
            ->where('assets_pivot.type', $type)
15
            ->where('assets_pivot.locale', $locale)
16
            ->get()
17
            ->each(function(Asset $asset) use($model, $orderedAssetIds, $type, $locale){
18
19
                DB::table('assets_pivot')
20
                    ->where('asset_id', $asset->id)
21
                    ->where('entity_type', $model->getMorphClass())
0 ignored issues
show
Bug introduced by
The method getMorphClass() does not exist on Thinktomorrow\AssetLibrary\HasAsset. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\AssetLibrary\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
                    ->where('entity_type', $model->/** @scrutinizer ignore-call */ getMorphClass())
Loading history...
22
                    ->where('entity_id', $model->getKey())
0 ignored issues
show
Bug introduced by
The method getKey() does not exist on Thinktomorrow\AssetLibrary\HasAsset. Since it exists in all sub-types, consider adding an abstract or default implementation to Thinktomorrow\AssetLibrary\HasAsset. ( Ignorable by Annotation )

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

22
                    ->where('entity_id', $model->/** @scrutinizer ignore-call */ getKey())
Loading history...
23
                    ->where('type', $type)
24
                    ->where('locale', $locale)
25
                    ->update([
26
                        'order' => array_search($asset->id, $orderedAssetIds),
27
                    ]);
28
            });
29
30
    }
31
32
    /**
33
     * Taken from: https://github.com/laravel/ideas/issues/575
34
     *
35
     * @return void
36
     */
37
    private static function batchUpdateColumn(string $table, string $column, array $indices, string $indexColumn = 'id', bool $castIdToIntegers = true, string $extraWhere = null): void
0 ignored issues
show
Unused Code introduced by
The method batchUpdateColumn() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
38
    {
39
        $cases = [];
40
        $ids = [];
41
        $params = [];
42
43
        foreach ($indices as $index => $modelId) {
44
            $id = $castIdToIntegers ? (int) $modelId : $modelId;
45
            $ids[] = "'{$id}'";
46
47
            $cases[] = "WHEN '{$id}' then ?";
48
            $params[] = $index;
49
        }
50
51
        $ids = implode(',', $ids);
52
        $cases = implode(' ', $cases);
53
54
        if ($extraWhere) {
55
            $extraWhere = ' AND ' . DB::raw($extraWhere);
56
        }
57
58
        DB::update("UPDATE `{$table}` SET `{$column}` = CASE `".$indexColumn."` {$cases} END WHERE `".$indexColumn."` in ({$ids})".$extraWhere, $params);
59
    }
60
}
61