Failed Conditions
Pull Request — experimental/3.1 (#2374)
by Kentaro
29:44
created

CalculateContext::buildCalculator()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
crap 3.072
1
<?php
2
namespace Eccube\Service\Calculator;
3
4
use Eccube\Entity\Order;
5
use Eccube\Entity\PurchaseInterface;
6
use Eccube\Entity\ShipmentItem;
7
use Eccube\Service\Calculator\Strategy\CalculateStrategyInterface;
8
9
class CalculateContext
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
10
{
11
    /* @var Order $Order */
12
    protected $Order;
13
14
    /* @var ShipmentItemCollection $ShipmentItems */
15 12
    protected $ShipmentItems = []; // Collection になってる?
16
17 12
    // $app['eccube.calculate.strategies'] に DI する
18 12
    /* @var \Eccube\Service\Calculator\CalculateStrategyCollection CalculateStrategies */
19
    protected $CalculateStrategies;
20
21 12
    public function executeCalculator()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
22 12
    {
23 12
        $this->buildCalculator($this->CalculateStrategies);
24 12
25
        /** @var ShipmentItem $ShipmentItem */
26
        foreach($this->ShipmentItems as $ShipmentItem) {
27
            if ($ShipmentItem instanceof ShipmentItem) {
28 12
                if (!$this->Order->getItems()->contains($ShipmentItem)) {
29
                    $ShipmentItem->setOrder($this->Order);
30
                    $this->Order->addShipmentItem($ShipmentItem);
31 12
                    // ここのタイミングで Persist 可能?
32
                }
33
            }
34 12
        }
35 12
        return $this->calculateOrder($this->Order);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
36 12
    }
37 12
38
    public function buildCalculator(\Eccube\Service\Calculator\CalculateStrategyCollection $strategies)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
39
    {
40 12
        foreach ($strategies as $Strategy) {
41 12
            if (in_array($this->ShipmentItems->getType(), $Strategy->getTargetTypes())) {
42 12
                $Strategy->execute($this->ShipmentItems);
43
            }
44
        }
45 12
    }
46
47 12
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Order" missing
Loading history...
48
     * TODO
49
     * 集計は全部ここでやる. 明細を加算するのみ.
50
     * 計算結果を Order にセットし直すのもここでやる.
51
     * DI で別クラスにした方がいいかも
52
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
53
    public function calculateOrder(PurchaseInterface $Order)
54
    {
55 12
        // OrderDetails の計算結果を Order にセットする
56
        if ($this->Order instanceof Order) { // TODO context のほうで判定したい
57 12
            $subTotal = $Order->calculateSubTotal();
58
            $Order->setSubtotal($subTotal);
59 12
            $total = $Order->getTotalPrice();
60
            if ($total < 0) {
61
                $total = 0;
62
            }
63
            $Order->setTotal($total);
64
            $Order->setPaymentTotal($total);
65
        }
66
        return $Order;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
67
    }
68
69
    public function setCalculateStrategies(\Eccube\Service\Calculator\CalculateStrategyCollection $strategies)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
70
    {
71
        $this->CalculateStrategies = $strategies;
72
    }
73
74
    public function getCalculateStrategies()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
75
    {
76
        return $this->CalculateStrategies;
77
    }
78
79
    public function setOrder(PurchaseInterface $Order)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
80
    {
81
        $this->Order = $Order;
0 ignored issues
show
Documentation Bug introduced by
It seems like $Order of type object<Eccube\Entity\PurchaseInterface> is incompatible with the declared type object<Eccube\Entity\Order> of property $Order.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
82
        $this->ShipmentItems = new ShipmentItemCollection($Order->getItems()->toArray(), get_class($this->Order));
83
    }
84
}
85