ServerSort::byServerName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Server module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-server
6
 * @package   hipanel-module-server
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\server\helpers;
12
13
use hipanel\modules\server\models\Consumption;
14
use hipanel\modules\server\models\Server;
15
use Tuck\Sort\Sort;
16
use Tuck\Sort\SortChain;
17
18
class ServerSort
19
{
20
    public static function byServerName(): SortChain
21
    {
22
        return Sort::chain()->compare(function (Server $a, Server $b) {
23
            return strnatcasecmp($a->name, $b->name);
24
        });
25
    }
26
27
    public static function byConsumptionType(): SortChain
28
    {
29
        $order = [
30
            'monthly,monthly',
31
            'monthly,rack',
32
            'monthly,rack_unit',
33
            'monthly,hardware',
34
            'monthly,server_traf_max',
35
            'monthly,server_traf95_max',
36
            'overuse,server_traf_max',
37
            'overuse,server_traf95_max',
38
            'monthly,support_time',
39
            'overuse,support_time',
40
            'monthly,backup_du',
41
            'overuse,backup_du',
42
            'monthly,ip_num',
43
            'overuse,ip_num',
44
        ];
45
46
        return Sort::chain()->asc(function (Consumption $consumption) use ($order) {
47
            if (($key = array_search($consumption->type, $order, true)) !== false) {
48
                return $key;
49
            }
50
51
            return INF;
52
        });
53
    }
54
}
55