1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Finance module for HiPanel |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
6
|
|
|
* @package hipanel-module-finance |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\helpers; |
12
|
|
|
|
13
|
|
|
use Closure; |
14
|
|
|
use hipanel\modules\finance\models\Price; |
15
|
|
|
use Tuck\Sort\Sort; |
16
|
|
|
use Tuck\Sort\SortChain; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class PriceSort provides sorting functions for prices. |
20
|
|
|
* |
21
|
|
|
* @author Dmytro Naumenko <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class PriceSort |
24
|
|
|
{ |
25
|
|
|
public static function anyPrices(): SortChain |
26
|
|
|
{ |
27
|
|
|
return Sort::chain() |
28
|
|
|
->compare(self::serverPrices()) |
29
|
|
|
->compare(self::byTargetObjectName()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return SortChain |
34
|
|
|
*/ |
35
|
|
|
public static function serverPrices(): SortChain |
36
|
|
|
{ |
37
|
|
|
return Sort::chain() |
38
|
|
|
->asc(self::byServerPriceGroups()) |
39
|
|
|
->asc(self::byObjectType()) |
40
|
|
|
->asc(self::byServerMainPrices()) |
41
|
|
|
->asc(self::byHardwareType()) |
42
|
|
|
->compare(self::byTargetObjectName()) |
43
|
|
|
->compare(self::byServerPriceType()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function zonePrices(): SortChain |
47
|
|
|
{ |
48
|
|
|
return Sort::chain()->asc(self::byObjectNo()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private static function byServerPriceType() |
52
|
|
|
{ |
53
|
|
|
return static function (Price $a, Price $b) { |
54
|
|
|
if ($a->getSubtype() === $b->getSubtype()) { |
55
|
|
|
return $a->isOveruse() ? 1 : -1; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return 0; |
59
|
|
|
}; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private static function byServerPriceGroups(): Closure |
63
|
|
|
{ |
64
|
|
|
return function (Price $price) { |
65
|
|
|
if ($price->type !== 'monthly,hardware') { |
66
|
|
|
return 1; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($price->getSubtype() === 'hardware') { |
70
|
|
|
return 2; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return INF; |
74
|
|
|
}; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private static function byServerMainPrices(): Closure |
78
|
|
|
{ |
79
|
|
|
$order = [ |
80
|
|
|
'rack', |
81
|
|
|
'rack_unit', |
82
|
|
|
'ip_num', |
83
|
|
|
'support_time', |
84
|
|
|
'backup_du', |
85
|
|
|
'server_traf_max', |
86
|
|
|
'cdn_traf_max', |
87
|
|
|
'server_traf95_max', |
88
|
|
|
'cdn_traf95_max', |
89
|
|
|
'server_du', |
90
|
|
|
'storage_du', |
91
|
|
|
'cdn_cache', |
92
|
|
|
'server_ssd', |
93
|
|
|
'server_sata', |
94
|
|
|
'win_license', |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
View Code Duplication |
return static function (Price $price) use ($order) { |
|
|
|
|
98
|
|
|
if (($key = array_search($price->getSubtype(), $order, true)) !== false) { |
99
|
|
|
return $key; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return INF; |
103
|
|
|
}; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
private static function byHardwareType(): Closure |
107
|
|
|
{ |
108
|
|
|
$order = ['SERVER', 'CHASSIS', 'MOTHERBOARD', 'CPU', 'RAM', 'HDD', 'SSD']; |
109
|
|
|
|
110
|
|
|
return function (Price $price) use ($order) { |
111
|
|
|
$type = substr($price->object->name, 0, strpos($price->object->name, ':')); |
112
|
|
|
if (($key = array_search($type, $order, true)) !== false) { |
113
|
|
|
return $key; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return INF; |
117
|
|
|
}; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private static function byTargetObjectName(): Closure |
121
|
|
|
{ |
122
|
|
|
return function (Price $a, Price $b) { |
123
|
|
|
return strnatcasecmp($a->object->name, $b->object->name); |
124
|
|
|
}; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private static function byObjectType(): Closure |
128
|
|
|
{ |
129
|
|
|
$order = ['dedicated', 'net', 'model_group', 'part']; |
130
|
|
|
|
131
|
|
View Code Duplication |
return function (Price $price) use ($order) { |
132
|
|
|
if (($key = array_search($price->object->type, $order, true)) !== false) { |
133
|
|
|
return $key; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return INF; |
137
|
|
|
}; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private static function byObjectNo(): Closure |
141
|
|
|
{ |
142
|
|
|
return static function ($group): int { |
143
|
|
|
return reset($group)->object->no; |
144
|
|
|
}; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
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.