Completed
Push — master ( 5e0ad4...117a3a )
by Dmitry
10:56
created

PriceSort::byServerPriceGroups()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 13
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 1
nop 0
crap 30
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
    /**
17
     * @return \Tuck\Sort\SortChain
18
     */
19
    public static function serverPrices(): SortChain
20
    {
21
        return Sort::chain()
22
            ->asc(self::byServerPriceGroups())
23
            ->asc(self::byServerMainPrices())
24
            ->asc(self::byHardwareType());
25
    }
26
27
    private static function byServerPriceGroups(): \Closure
28
    {
29
        return function (Price $price) {
30
            if (!$price->isOveruse() && $price->type !== 'monthly,hardware') {
31
                return 1;
32
            } elseif ($price->isOveruse()) {
33
                return 2;
34
            } elseif ($price->getSubtype() === 'hardware') {
35
                return 3;
36
            }
37
38
            return INF;
39
        };
40
    }
41
42
    private static function byServerMainPrices(): \Closure
43
    {
44
        $order = ['rack_unit', 'ip_num', 'support_time', 'backup_du', 'server_traf_max', 'server_traf95_max'];
45
46
        return function (Price $price) use ($order) {
47
            if (($key = array_search($price->getSubtype(), $order)) !== false) {
48
                return $key;
49
            }
50
51
            return INF;
52
        };
53
    }
54
55
    private static function byHardwareType(): \Closure
56
    {
57
        $order = ['CHASSIS', 'MOTHERBOARD', 'CPU', 'RAM', 'HDD', 'SSD'];
58
59
        return function (Price $price) use ($order) {
60
            $type = substr($price->object->name, 0, strpos($price->object->name, ':'));
61
            if (($key = array_search($type, $order)) !== false) {
62
                return $key;
63
            }
64
65
            return INF;
66
        };
67
    }
68
}
69