Sorter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A bootSorter() 0 6 1
B setSorterOrder() 0 19 5
A getSortOrderColumn() 0 4 2
1
<?php
2
/*
3
 * This file is part of the Laravel Platfourm package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\Platfourm\Database\Eloquent\Traits;
12
13
use Exception;
14
15
/**
16
 * Sorter model trait.
17
 *
18
 * Usage:
19
 *
20
 * Model table must have sort_order table column.
21
 *
22
 * In the model class definition:
23
 *
24
 *   use Longman\Platfourm\Database\Eloquent\Traits\Sorter;
25
 *
26
 * To set orders:
27
 *
28
 *   $model->setSorterOrder($recordIds, $recordOrders);
29
 *
30
 * You can change the sort field used by declaring:
31
 *
32
 *   const SORT_ORDER = 'my_sort_order';
33
 */
34
trait Sorter
35
{
36
    /**
37
     * Boot the Sorter trait for this model.
38
     *
39
     * @return void
40
     */
41
    public static function bootSorter()
42
    {
43
        static::created(function ($model) {
44
            $model->setSorterOrder($model->id);
45
        });
46
    }
47
48
    /**
49
     * Sets the sort order of records to the specified orders. If the orders is
50
     * undefined, the record identifier is used.
51
     */
52
    public function setSorterOrder($itemIds, $itemOrders = null)
53
    {
54
        if (!is_array($itemIds)) {
55
            $itemIds = [$itemIds];
56
        }
57
58
        if ($itemOrders === null) {
59
            $itemOrders = $itemIds;
60
        }
61
62
        if (count($itemIds) != count($itemOrders)) {
63
            throw new Exception('Invalid setSorterOrder call - count of itemIds do not match count of itemOrders');
64
        }
65
66
        foreach ($itemIds as $index => $id) {
67
            $order = $itemOrders[$index];
68
            $this->newQuery()->where('id', $id)->update([$this->getSortOrderColumn() => $order]);
0 ignored issues
show
Bug introduced by
It seems like newQuery() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
69
        }
70
    }
71
72
    /**
73
     * Get the name of the "sort order" column.
74
     *
75
     * @return string
76
     */
77
    public function getSortOrderColumn()
78
    {
79
        return defined('static::SORT_ORDER') ? static::SORT_ORDER : 'sort_order';
80
    }
81
}
82