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

ReorderAssets::batchUpdateColumn()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 13
c 1
b 0
f 0
nc 6
nop 6
dl 0
loc 22
rs 9.8333
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