Completed
Push — master ( 6fb6f5...c08a9c )
by Dmitry
11:30
created

PriceSort   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 69
ccs 0
cts 47
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A anyPrices() 0 6 1
A serverPrices() 0 7 1
B byServerPriceGroups() 0 14 5
A byServerMainPrices() 0 12 2
A byHardwareType() 0 13 2
A byTargetObjectName() 0 6 1
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::byTargetObjectName())
20
            ->compare(self::serverPrices());
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
    }
33
34
    private static function byServerPriceGroups(): \Closure
35
    {
36
        return function (Price $price) {
37
            if (!$price->isOveruse() && $price->type !== 'monthly,hardware') {
38
                return 1;
39
            } elseif ($price->isOveruse()) {
40
                return 2;
41
            } elseif ($price->getSubtype() === 'hardware') {
42
                return 3;
43
            }
44
45
            return INF;
46
        };
47
    }
48
49
    private static function byServerMainPrices(): \Closure
50
    {
51
        $order = ['rack_unit', 'ip_num', 'support_time', 'backup_du', 'server_traf_max', 'server_traf95_max'];
52
53
        return function (Price $price) use ($order) {
54
            if (($key = array_search($price->getSubtype(), $order)) !== false) {
55
                return $key;
56
            }
57
58
            return INF;
59
        };
60
    }
61
62
    private static function byHardwareType(): \Closure
63
    {
64
        $order = ['CHASSIS', 'MOTHERBOARD', 'CPU', 'RAM', 'HDD', 'SSD'];
65
66
        return function (Price $price) use ($order) {
67
            $type = substr($price->object->name, 0, strpos($price->object->name, ':'));
68
            if (($key = array_search($type, $order)) !== false) {
69
                return $key;
70
            }
71
72
            return INF;
73
        };
74
    }
75
76
    private static function byTargetObjectName(): \Closure
77
    {
78
        return function (Price $a, Price $b) {
79
            return strnatcasecmp($a->object->name, $b->object->name);
80
        };
81
    }
82
}
83