Completed
Push — master ( 958dbc...2d3a59 )
by Dmitry
05:33
created

PlanInternalsGrouper::groupServerPrices()   B

Complexity

Conditions 9
Paths 80

Size

Total Lines 54
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 7.255
c 0
b 0
f 0
cc 9
eloc 33
nc 80
nop 0

How to fix   Long Method   

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