Completed
Push — master ( 0ac49a...440acf )
by Dmitry
07:30
created

CartFinisher   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 9
dl 0
loc 106
ccs 0
cts 56
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSuccess() 0 4 1
A getError() 0 4 1
A getPending() 0 4 1
C run() 0 28 7
B ensureCanBeFinished() 0 23 6
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\hiart\ResponseErrorException;
14
use hiqdev\yii2\cart\ShoppingCart;
15
use Yii;
16
use yii\base\BaseObject;
17
18
class CartFinisher extends BaseObject
19
{
20
    /**
21
     * @var ShoppingCart
22
     */
23
    public $cart;
24
25
    /**
26
     * @var AbstractPurchase[] array of successful purchases
27
     */
28
    protected $_success = [];
29
30
    /**
31
     * @var ErrorPurchaseException[] array of failed purchases
32
     */
33
    protected $_error = [];
34
35
    /**
36
     * @var PendingPurchaseException[] array of purchases that are pending
37
     */
38
    protected $_pending = [];
39
40
    /**
41
     * Getter for array of successful purchases.
42
     * @return AbstractPurchase[]
43
     */
44
    public function getSuccess()
45
    {
46
        return $this->_success;
47
    }
48
49
    /**
50
     * Getter for array of failed purchases.
51
     * @return ErrorPurchaseException[]
52
     */
53
    public function getError()
54
    {
55
        return $this->_error;
56
    }
57
58
    /**
59
     * Getter for array of failed purchases.
60
     * @return PendingPurchaseException[]
61
     */
62
    public function getPending()
63
    {
64
        return $this->_pending;
65
    }
66
67
    /**
68
     * Runs the purchase.
69
     * Purchases the positions in the [[cart]].
70
     */
71
    public function run()
72
    {
73
        if ($this->cart->isEmpty) {
74
            return;
75
        }
76
77
        $this->ensureCanBeFinished();
78
79
        foreach ($this->cart->positions as $position) {
80
            $purchase = $position->getPurchaseModel();
81
82
            try {
83
                if ($purchase->execute()) {
84
                    $this->_success[] = $purchase;
85
                    $this->cart->remove($position);
86
                } else {
87
                    $this->_error[] = new ErrorPurchaseException(reset(reset($purchase->getErrors())), $purchase);
0 ignored issues
show
Bug introduced by
$purchase->getErrors() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
Bug introduced by
reset($purchase->getErrors()) cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
88
                }
89
            } catch (PendingPurchaseException $e) {
90
                $this->_pending[] = $e;
91
                $this->cart->remove($position);
92
            } catch (ResponseErrorException $e) {
93
                $this->_error[] = new ErrorPurchaseException($e->getMessage(), $purchase, $e);
94
            } catch (\hiqdev\hiart\Exception $e) {
95
                $this->_error[] = new ErrorPurchaseException($e->getMessage(), $purchase, $e);
96
            }
97
        }
98
    }
99
100
    protected function ensureCanBeFinished()
101
    {
102
        /** @var PositionPurchasabilityValidatorInterface[] $validators */
103
        $validators = [];
104
105
        foreach ($this->cart->positions as $position) {
106
            $purchase = $position->getPurchaseModel();
107
108
            foreach ($purchase->getPurchasabilityRules() as $validator) {
109
                if (!isset($validators[$validator])) {
110
                    $validators[$validator] = Yii::createObject($validator);
111
                }
112
            }
113
        }
114
115
        try {
116
            foreach ($validators as $validator) {
117
                $validator->validate($this->cart->positions);
118
            }
119
        } catch (NotPurchasablePositionException $e) {
120
            $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...
121
        }
122
    }
123
}
124