ChargeSort::anyCharges()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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 hipanel\modules\finance\models\Charge;
14
use Tuck\Sort\Sort;
15
use Tuck\Sort\SortChain;
16
17
/**
18
 * Class PriceSort provides sorting functions for prices.
19
 *
20
 * @author Dmytro Naumenko <[email protected]>
21
 */
22
class ChargeSort
23
{
24
    public static function anyCharges(): SortChain
25
    {
26
        return Sort::chain()
27
            ->asc(self::byType())
28
            ->asc(self::byHardwareType())
29
            ->compare(self::byName())
30
            ->asc(self::keepDiscountsWithParents());
31
    }
32
33 View Code Duplication
    private static function byHardwareType(): \Closure
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
34
    {
35
        $order = ['SERVER', 'CHASSIS', 'MOTHERBOARD', 'CPU', 'RAM', 'HDD', 'SSD'];
36
37
        return function (Charge $charge) use ($order) {
38
            if ($charge->class === 'part') {
39
                $type = substr($charge->name, 0, strpos($charge->name, ':'));
40
                if (($key = array_search($type, $order, true)) !== false) {
41
                    return $key;
42
                }
43
            }
44
45
            return INF;
46
        };
47
    }
48
49
    private static function byType(): \Closure
50
    {
51
        $order = [
52
            'rack',
53
            'rack_unit',
54
            'ip_num',
55
            'support_time',
56
            'backup_du',
57
            'server_traf_max',
58
            'server_traf95_max',
59
            'server_du',
60
            'server_ssd',
61
            'server_sata',
62
            'win_license',
63
        ];
64
65
        return function (Charge $charge) use ($order) {
66
            if (($key = array_search($charge->type, $order, true)) !== false) {
67
                return $key;
68
            }
69
70
            return INF;
71
        };
72
    }
73
74
    private static function keepDiscountsWithParents(): \Closure
75
    {
76
        return function (Charge $charge) {
77
            $a = $charge->parent_id !== null
78
                ? $charge->parent_id * 10 + 2
79
                : $charge->id * 10 + 1;
80
81
            return $a;
82
        };
83
    }
84
85
    private static function byName(): \Closure
86
    {
87
        return function (Charge $a, Charge $b) {
88
            return strnatcasecmp($a->name, $b->name);
89
        };
90
    }
91
}
92