Completed
Push — master ( bc8cc8...6e347a )
by Dmitry
04:49
created

CartFinisher::run()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 18
cp 0
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 12
nc 6
nop 0
crap 30
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\cart;
12
13
use hiqdev\yii2\cart\ShoppingCart;
14
use Yii;
15
use yii\base\BaseObject;
16
17
class CartFinisher extends BaseObject
18
{
19
    /**
20
     * @var ShoppingCart
21
     */
22
    public $cart;
23
24
    /**
25
     * @var PurchaseStrategyInterface[]
26
     */
27
    protected $purchasers = [];
28
29
    /**
30
     * @var AbstractPurchase[] array of successful purchases
31
     */
32
    protected $_success = [];
33
34
    /**
35
     * @var ErrorPurchaseException[] array of failed purchases
36
     */
37
    protected $_error = [];
38
39
    /**
40
     * @var PendingPurchaseException[] array of purchases that are pending
41
     */
42
    protected $_pending = [];
43
44
    /**
45
     * Getter for array of successful purchases.
46
     * @return AbstractPurchase[]
47
     */
48
    public function getSuccess()
49
    {
50
        return $this->_success;
51
    }
52
53
    /**
54
     * Getter for array of failed purchases.
55
     * @return ErrorPurchaseException[]
56
     */
57
    public function getError()
58
    {
59
        return $this->_error;
60
    }
61
62
    /**
63
     * Getter for array of failed purchases.
64
     * @return PendingPurchaseException[]
65
     */
66
    public function getPending()
67
    {
68
        return $this->_pending;
69
    }
70
71
    /**
72
     * Runs the purchase.
73
     * Purchases the positions in the [[cart]].
74
     */
75
    public function run()
76
    {
77
        if ($this->cart->isEmpty) {
78
            return;
79
        }
80
81
        $this->ensureCanBeFinished();
82
        $this->createPurchasers();
83
84
        foreach ($this->purchasers as $purchaser) {
85
            $purchaser->run();
86
87
            foreach ($this->_success = $purchaser->getSuccessPurchases() as $purchase) {
88
                $this->cart->remove($purchase->position);
89
            }
90
            foreach ($this->_pending = $purchaser->getPendingPurchaseExceptions() as $exception) {
91
                $this->cart->remove($exception->position);
92
            }
93
            $this->_error = $purchaser->getErrorPurchaseExceptions();
94
        }
95
    }
96
97
    protected function ensureCanBeFinished()
98
    {
99
        /** @var PositionPurchasabilityValidatorInterface[] $validators */
100
        $validators = [];
101
102
        foreach ($this->cart->positions as $position) {
103
            $purchase = $position->getPurchaseModel();
104
105
            foreach ($purchase->getPurchasabilityRules() as $validator) {
106
                if (!isset($validators[$validator])) {
107
                    $validators[$validator] = Yii::createObject($validator);
108
                }
109
            }
110
        }
111
112
        try {
113
            foreach ($validators as $validator) {
114
                $validator->validate($this->cart->positions);
115
            }
116
        } catch (NotPurchasablePositionException $e) {
117
            $e->resolve();
0 ignored issues
show
Unused Code introduced by
The call to the method hipanel\modules\finance\...ionException::resolve() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
118
        }
119
    }
120
121
    protected function createPurchasers()
122
    {
123
        foreach ($this->cart->positions as $position) {
124
            if ($position instanceof BatchPurchasablePositionInterface) {
125
                $purchaser = $this->getPurchaser($position->getBatchPurchaseStrategyClass());
126
            } else {
127
                $purchaser = $this->getPurchaser(OneByOnePurchaseStrategy::class);
128
            }
129
130
            $purchaser->addPosition($position);
131
        }
132
    }
133
134
    /**
135
     * @param string $class
136
     * @return PurchaseStrategyInterface
137
     */
138
    protected function getPurchaser($class)
139
    {
140
        if (!isset($this->purchasers[$class])) {
141
            $this->purchasers[$class] = new $class($this->cart);
142
        }
143
144
        return $this->purchasers[$class];
145
    }
146
}
147