Completed
Push — master ( d9c24b...dc8c4b )
by Dmitry
12:22
created

CartCalculator::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 2
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * Finance module for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-finance
7
 * @package   hipanel-module-finance
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\finance\cart;
13
14
use hipanel\modules\finance\logic\Calculator;
15
use hipanel\modules\finance\models\Calculation;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, hipanel\modules\finance\cart\Calculation.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
use hipanel\modules\finance\models\Value;
17
use hiqdev\yii2\cart\ShoppingCart;
18
use Yii;
19
use yz\shoppingcart\CartActionEvent;
20
21
/**
22
 * Class CartCalculator provides API to calculate [[cart]] positions value
23
 *
24
 * Usage:
25
 *
26
 * ```php
27
 * $calculator = new CartCalculator([
28
 *     'cart' => $this->cart
29
 * ]);
30
 *
31
 * $calculator->run(); // will calculate prices for all cart positions and update them
32
 * ```
33
 *
34
 * Also can be bound to some cart event as handler:
35
 *
36
 * ```php
37
 * $cart->on(Cart::EVENT_UPDATE, [CartCalculator::class, 'handle']);
38
 * ```
39
 *
40
 * @package hipanel\modules\finance\cart
41
 */
42
class CartCalculator extends Calculator
43
{
44
    /**
45
     * @var AbstractCartPosition[]
46
     */
47
    protected $models;
48
49
    /**
50
     * @var ShoppingCart
51
     */
52
    public $cart;
53
54
    /**
55
     * @var CartActionEvent
56
     */
57
    public $event;
58
59
    /**
60
     * Creates the instance of the object and runs the calculation.
61
     *
62
     * @param CartActionEvent $event The event
63
     * @void
64
     */
65
    public static function handle($event)
66
    {
67
        /** @var ShoppingCart $cart */
68
        $cart = $event->sender;
69
70
        $calculator = new static($cart);
71
        return $calculator->execute();
72
    }
73
74
    /**
75
     * @param ShoppingCart $cart
76
     */
77
    public function __construct(ShoppingCart $cart)
78
    {
79
        $this->cart = $cart;
80
81
        parent::__construct($this->cart->positions);
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function execute()
88
    {
89
        parent::execute();
90
91
        $this->applyCalculations();
92
93
        return $this->calculations;
94
    }
95
96
    /**
97
     * Updates positions using the calculations provided with [[getCalculation]]
98
     */
99
    private function applyCalculations()
100
    {
101
        $currency = Yii::$app->params['currency'];
102
103
        foreach ($this->models as $position) {
104
            $id = $position->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\f...t\AbstractCartPosition>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
105
106
            $calculation = $this->getCalculation($id);
107
            if (!$calculation instanceof Calculation) {
108
                Yii::error('Cart position "' . $position->getName() . '" was removed from the cart because of failed value calculation. Normally this should never happen.', 'hipanel.cart');
109
                $this->cart->removeById($position->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\f...t\AbstractCartPosition>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
110
                break;
111
            }
112
113
            $value = $calculation->forCurrency($currency);
114
            if (!$value instanceof Value) {
115
                Yii::error('Cart position "' . $position->getName() . '" was removed from the cart because calculation for currency "' . $currency . '" is not available', 'hipanel.cart');
116
                $this->cart->removeById($position->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<hipanel\modules\f...t\AbstractCartPosition>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
117
                break;
118
            }
119
120
            $position->setPrice($value->price);
0 ignored issues
show
Documentation introduced by
The property price does not exist on object<hipanel\modules\finance\models\Value>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
121
            $position->setValue($value->value);
0 ignored issues
show
Documentation introduced by
The property value does not exist on object<hipanel\modules\finance\models\Value>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
122
        }
123
    }
124
}
125