Completed
Push — master ( 5a5e4b...77b1bb )
by Dmitry
06:22
created

TariffCalculator::getCalculation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 0
cts 1
cp 0
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace hipanel\modules\finance\logic;
4
5
use hipanel\base\Model;
6
use hipanel\modules\finance\models\Calculation;
7
use hipanel\modules\finance\models\CalculableModelInterface;
8
use hiqdev\hiart\ErrorResponseException;
9
use yii\web\UnprocessableEntityHttpException;
10
11
class TariffCalculator
12
{
13
    /**
14
     * @var Model[]|CalculableModelInterface[]
15
     */
16
    private $tariffs;
17
18
    /**
19
     * @var Calculation[]
20
     */
21
    private $calculations;
22
23
    /**
24
     * TariffCalculator constructor.
25
     * @param Model[]|Model $tariffs
26
     */
27
    public function __construct($tariffs)
28
    {
29
        if ($tariffs instanceof Model) {
30
            $tariffs = [$tariffs];
31
        }
32
33
        $this->tariffs = $tariffs;
34
    }
35
36
    /**
37
     * Gets [[Calculation]] for the $tariffId
38
     *
39
     * @param integer $tariffId
40
     * @return Calculation
41
     */
42
    public function getCalculation($tariffId)
43
    {
44
        if ($this->calculations === null) {
45
            $this->execute();
46
        }
47
48
        return $this->calculations[$tariffId];
49
    }
50
51
    /**
52
     * @return Calculation[]
53
     */
54
    public function getCalculations()
55
    {
56
        if ($this->calculations === null) {
57
            $this->execute();
58
        }
59
60
        return $this->calculations;
61
    }
62
63
    /**
64
     * @return \hipanel\modules\finance\models\Calculation[]
65
     * @throws UnprocessableEntityHttpException
66
     */
67
    public function execute()
68
    {
69
        try {
70
            $rows = Calculation::perform('CalcValue', $this->collectData(), true);
71
        } catch (\Exception $e) {
72
            throw new UnprocessableEntityHttpException('Failed to calculate tariffs value', 0, $e);
73
        }
74
75
        $this->calculations = $this->createCalculations($rows);
76
77
        return $this->calculations;
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    private function collectData()
84
    {
85
        $data = [];
86
        foreach ($this->tariffs as $tariff) {
87
            $calculation = $tariff->getCalculationModel();
0 ignored issues
show
Bug introduced by
The method getCalculationModel does only exist in hipanel\modules\finance\...alculableModelInterface, but not in hipanel\base\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
88
            $data[$tariff->getPrimaryKey()] = $calculation->getAttributes();
0 ignored issues
show
Bug introduced by
The method getPrimaryKey does only exist in hipanel\base\Model, but not in hipanel\modules\finance\...alculableModelInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
89
        }
90
        return $data;
91
    }
92
93
    /**
94
     * @param $rows
95
     * @return \hipanel\modules\finance\models\Calculation[]
96
     */
97
    private function createCalculations($rows)
98
    {
99
        $query = Calculation::find()->joinWith(['value'])->indexBy('tariff_id')->prepare();
100
        return $query->populate($rows);
101
    }
102
}
103