Completed
Pull Request — master (#20)
by Klochok
12:09
created

RelatedPosition::setWidget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace hiqdev\yii2\cart;
4
5
use hipanel\modules\finance\logic\Calculator;
6
use yii\base\Widget;
7
use hiqdev\yii2\cart\ShoppingCart;
8
use hiqdev\yii2\cart\CartPositionInterface;
9
use Yii;
10
11
abstract class RelatedPosition implements RelatedPositionInterface
12
{
13
    /** @var Widget */
14
    public $widget;
15
16
    /** @var CartPositionInterface */
17
    public $mainPosition;
18
19
    /** @var ShoppingCart */
20
    public $cart;
21
22
    public function __construct(CartPositionInterface $mainPosition)
23
    {
24
        $this->cart = Module::getInstance()->getCart();
25
        $this->mainPosition = $mainPosition;
26
    }
27
28
    /** @inheritDoc */
29
    public function setWidget($className, array $params = []): RelatedPositionInterface
30
    {
31
        $this->widget = Yii::createObject(array_merge($params, [
32
            'class' => $className,
33
            'relatedPosition' => $this->createRelatedPosition(),
34
            'mainPosition' => $this->mainPosition,
35
            'cart' => $this->cart,
36
        ]));
37
38
        return $this;
39
    }
40
41
    /** @inheritDoc */
42
    public function render(): string
43
    {
44
        return $this->widget->run();
45
    }
46
47
    public function calculate(CartPositionInterface $position): CartPositionInterface
48
    {
49
        $calculator = new Calculator([$position]);
50
        $calculationId = $position->getCalculationModel()->calculation_id;
0 ignored issues
show
Bug introduced by
The method getCalculationModel() does not seem to exist on object<hiqdev\yii2\cart\CartPositionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
        $calculation = $calculator->getCalculation($calculationId);
52
        $value = $calculation->forCurrency($this->cart->getCurrency());
53
        $position->setPrice($value->price);
0 ignored issues
show
Bug introduced by
The method setPrice() does not seem to exist on object<hiqdev\yii2\cart\CartPositionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
        $position->setValue($value->value);
0 ignored issues
show
Bug introduced by
The method setValue() does not seem to exist on object<hiqdev\yii2\cart\CartPositionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
        $position->setCurrency($value->currency);
0 ignored issues
show
Bug introduced by
The method setCurrency() does not seem to exist on object<hiqdev\yii2\cart\CartPositionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
57
        return $position;
58
    }
59
60
}
61