Completed
Push — master ( 182b21...5283a1 )
by Dmitry
05:15
created

PriceSort::byServerPriceGroups()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 1
nop 0
crap 12
1
<?php
2
3
namespace hipanel\modules\finance\helpers;
4
5
use hipanel\modules\finance\models\Price;
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 PriceSort
15
{
16
    public static function anyPrices(): SortChain
17
    {
18
        return Sort::chain()
19
            ->compare(self::serverPrices())
20
            ->compare(self::byTargetObjectName());
21
    }
22
23
    /**
24
     * @return \Tuck\Sort\SortChain
25
     */
26
    public static function serverPrices(): SortChain
27
    {
28
        return Sort::chain()
29
            ->asc(self::byServerPriceGroups())
30
            ->asc(self::byServerMainPrices())
31
            ->asc(self::byHardwareType())
32
            ->compare(self::byServerPriceType());
33
    }
34
35
    private static function byServerPriceType()
36
    {
37
        return function (Price $a, Price $b) {
38
            if ($a->getSubtype() === $b->getSubtype()) {
39
                return $a->isOveruse() ? 1 : -1;
40
            }
41
42
            return 0;
43
        };
44
    }
45
46
    private static function byServerPriceGroups(): \Closure
47
    {
48
        return function (Price $price) {
49
            if ($price->type !== 'monthly,hardware') {
50
                return 1;
51
            } elseif ($price->getSubtype() === 'hardware') {
52
                return 2;
53
            }
54
55
            return INF;
56
        };
57
    }
58
59
    private static function byServerMainPrices(): \Closure
60
    {
61
        $order = ['rack_unit', 'ip_num', 'support_time', 'backup_du', 'server_traf_max', 'server_traf95_max'];
62
63
        return function (Price $price) use ($order) {
64
            if (($key = array_search($price->getSubtype(), $order)) !== false) {
65
                return $key;
66
            }
67
68
            return INF;
69
        };
70
    }
71
72
    private static function byHardwareType(): \Closure
73
    {
74
        $order = ['CHASSIS', 'MOTHERBOARD', 'CPU', 'RAM', 'HDD', 'SSD'];
75
76
        return function (Price $price) use ($order) {
77
            $type = substr($price->object->name, 0, strpos($price->object->name, ':'));
78
            if (($key = array_search($type, $order)) !== false) {
79
                return $key;
80
            }
81
82
            return INF;
83
        };
84
    }
85
86
    private static function byTargetObjectName(): \Closure
87
    {
88
        return function (Price $a, Price $b) {
89
            return strnatcasecmp($a->object->name, $b->object->name);
90
        };
91
    }
92
}
93