Completed
Push — master ( 195581...8d29bf )
by Andrii
10:41
created

PriceSort::zonePrices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
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 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 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 View Code Duplication
    private static function byServerMainPrices(): 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...
78
    {
79
        $order = [
80
            'rack',
81
            'rack_unit',
82
            'ip_num',
83
            'support_time',
84
            'backup_du',
85
            'server_traf_max',
86
            'server_traf95_max',
87
            'server_du',
88
            'server_ssd',
89
            'server_sata',
90
            'win_license',
91
        ];
92
93
        return function (Price $price) use ($order) {
94
            if (($key = array_search($price->getSubtype(), $order, true)) !== false) {
95
                return $key;
96
            }
97
98
            return INF;
99
        };
100
    }
101
102 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...
103
    {
104
        $order = ['SERVER', 'CHASSIS', 'MOTHERBOARD', 'CPU', 'RAM', 'HDD', 'SSD'];
105
106
        return function (Price $price) use ($order) {
107
            $type = substr($price->object->name, 0, strpos($price->object->name, ':'));
108
            if (($key = array_search($type, $order, true)) !== false) {
109
                return $key;
110
            }
111
112
            return INF;
113
        };
114
    }
115
116
    private static function byTargetObjectName(): Closure
117
    {
118
        return function (Price $a, Price $b) {
119
            return strnatcasecmp($a->object->name, $b->object->name);
120
        };
121
    }
122
123
    private static function byObjectType(): Closure
124
    {
125
        $order = ['dedicated', 'net', 'model_group', 'part'];
126
127
        return function (Price $price) use ($order) {
128
            if (($key = array_search($price->object->type, $order, true)) !== false) {
129
                return $key;
130
            }
131
132
            return INF;
133
        };
134
    }
135
136
    private static function byObjectNo(): Closure
137
    {
138
        return static function ($group): int {
139
            return reset($group)->object->no;
140
        };
141
    }
142
}
143