Completed
Push — master ( 5e0ad4...117a3a )
by Dmitry
10:56
created

PlanInternalsGrouper::groupServerPrices()   C

Complexity

Conditions 10
Paths 160

Size

Total Lines 60
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 60
ccs 0
cts 46
cp 0
rs 6.08
c 0
b 0
f 0
cc 10
eloc 36
nc 160
nop 0
crap 110

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace hipanel\modules\finance\helpers;
4
5
use hipanel\modules\finance\models\FakeSale;
6
use hipanel\modules\finance\models\Plan;
7
use hipanel\modules\finance\models\Price;
8
use hipanel\modules\finance\models\Sale;
9
use Tuck\Sort\Sort;
10
use Yii;
11
12
/**
13
 * Class PlanInternalsGrouper can be used to group prices inside $plan depending on
14
 * different factors.
15
 *
16
 * @author Dmytro Naumenko <[email protected]>
17
 */
18
class PlanInternalsGrouper
19
{
20
    /**
21
     * @var Plan
22
     */
23
    private $plan;
24
25
    public function __construct(Plan $plan)
26
    {
27
        $this->plan = $plan;
28
    }
29
30
    /**
31
     * Should be used to group prices of [[Plan]] with the following types:
32
     * - server
33
     * - sVDS
34
     * - oVDS
35
     *
36
     * @return array of two elements:
37
     * 0: sales, grouped by sold object
38
     * 1: prices, grouped by sold object
39
     */
40
    public function groupServerPrices()
41
    {
42
        $model = $this->plan;
43
        /** @var Sale[] $salesByObject */
44
        $salesByObject = [];
45
        /** @var Price[][] $pricesByMainObject */
46
        $pricesByMainObject = [];
47
48
        foreach ($model->prices as $price) {
49
            $pricesByMainObject[$price->main_object_id ?? $model->id][$price->id] = $price;
50
        }
51
52
        if (isset($pricesByMainObject[null])) {
53
            $salesByObject[null] = new FakeSale([
54
                'object' => Yii::t('hipanel.finance.price', 'Applicable for all objects'),
55
                'tariff_id' => $model->id,
56
            ]);
57
        }
58
        if (isset($pricesByMainObject[$model->id])) {
59
            $salesByObject[$model->id] = new FakeSale([
60
                'object' => Yii::t('hipanel.finance.price', 'For the whole tariff'),
61
                'tariff_id' => $model->id,
62
                'object_id' => $model->id,
63
            ]);
64
        }
65
        foreach ($model->sales as $sale) {
66
            $salesByObject[$sale->object_id] = $sale;
67
        }
68
69
        foreach ($pricesByMainObject as $id => $prices) {
70
            if (isset($salesByObject[$id])) {
71
                continue;
72
            }
73
74
            foreach ($prices as $price) {
75
                if ((int)$price->object_id === (int)$id) {
76
                    $salesByObject[$id] = new FakeSale([
77
                        'object' => $price->object->name,
78
                        'tariff_id' => $model->id,
79
                        'object_id' => $price->object_id,
80
                        'tariff_type' => $model->type,
81
                    ]);
82
                    continue 2;
83
                }
84
            }
85
86
            $salesByObject[$id] = new FakeSale([
87
                'object' => Yii::t('hipanel.finance.price', 'Unknown object name - no direct object prices exist'),
88
                'tariff_id' => $model->id,
89
                'object_id' => $id,
90
                'tariff_type' => $model->type,
91
            ]);
92
        }
93
94
        foreach ($pricesByMainObject as &$objPrices) {
95
            $objPrices = PriceSort::serverPrices()->values($objPrices, true);
96
        }
97
98
        return [$salesByObject, $pricesByMainObject];
99
    }
100
}
101