Completed
Push — master ( 8889ab...4adc7b )
by Dmitry
05:48
created

ChargeSort::anyCharges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\helpers;
4
5
use hipanel\modules\finance\models\Charge;
6
use Tuck\Sort\Sort;
7
use Tuck\Sort\SortChain;
8
9
/**
10
 * Class PriceSort provides sorting functions for prices.
11
 *
12
 * @author Dmytro Naumenko <[email protected]>
13
 */
14
class ChargeSort
15
{
16
    public static function anyCharges(): SortChain
17
    {
18
        return Sort::chain()
19
            ->asc(self::byType())
20
            ->asc(self::keepDiscountsWithParents());
21
    }
22
23 View Code Duplication
    private static function byType(): \Closure
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
    {
25
        $order = ['rack_unit', 'ip_num', 'support_time', 'backup_du', 'server_traf_max', 'server_traf95_max'];
26
27
        return function (Charge $charge) use ($order) {
28
            if (($key = array_search($charge->type, $order, true)) !== false) {
29
                return $key;
30
            }
31
32
            return INF;
33
        };
34
    }
35
36
    private static function keepDiscountsWithParents(): \Closure
37
    {
38
        return function (Charge $charge) {
39
            $a = $charge->parent_id !== null
40
                ? $charge->parent_id * 10 + 2
41
                : $charge->id * 10 + 1;
42
43
            return $a;
44
        };
45
    }
46
}
47