Completed
Push — master ( b86059...b2e8e3 )
by Andrii
07:55 queued 13s
created

src/cart/CartFinisher.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\cart;
12
13
use hiqdev\yii2\cart\NotPurchasableException;
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 PurchaseStrategyInterface[]
27
     */
28
    protected $purchasers = [];
29
30
    /**
31
     * @var AbstractPurchase[] array of successful purchases
32
     */
33
    protected $_success = [];
34
35
    /**
36
     * @var ErrorPurchaseException[] array of failed purchases
37
     */
38
    protected $_error = [];
39
40
    /**
41
     * @var PendingPurchaseException[] array of purchases that are pending
42
     */
43
    protected $_pending = [];
44
45
    /**
46
     * Getter for array of successful purchases.
47
     * @return AbstractPurchase[]
48
     */
49
    public function getSuccess()
50
    {
51
        return $this->_success;
52
    }
53
54
    /**
55
     * Getter for array of failed purchases.
56
     * @return ErrorPurchaseException[]
57
     */
58
    public function getError()
59
    {
60
        return $this->_error;
61
    }
62
63
    /**
64
     * Getter for array of failed purchases.
65
     * @return PendingPurchaseException[]
66
     */
67
    public function getPending()
68
    {
69
        return $this->_pending;
70
    }
71
72
    /**
73
     * Runs the purchase.
74
     * Purchases the positions in the [[cart]].
75
     */
76
    public function run()
77
    {
78
        if ($this->cart->isEmpty) {
79
            return;
80
        }
81
82
        $this->ensureCanBeFinished();
83
        $this->createPurchasers();
84
85
        foreach ($this->purchasers as $purchaser) {
86
            $purchaser->run();
87
88
            $this->_success = array_merge($this->_success, $purchaser->getSuccessPurchases());
89
            foreach ($purchaser->getSuccessPurchases() as $purchase) {
90
                $this->cart->remove($purchase->position);
91
            }
92
            $this->_pending = array_merge($this->_pending, $purchaser->getPendingPurchaseExceptions());
93
            foreach ($purchaser->getPendingPurchaseExceptions() as $exception) {
94
                $this->cart->remove($exception->position);
95
            }
96
            $this->_error = array_merge($this->_error, $purchaser->getErrorPurchaseExceptions());
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 (NotPurchasableException $e) {
0 ignored issues
show
The class hiqdev\yii2\cart\NotPurchasableException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
120
            $e->resolve();
121
        }
122
    }
123
124
    protected function createPurchasers()
125
    {
126
        foreach ($this->cart->positions as $position) {
127
            if ($position instanceof BatchPurchasablePositionInterface) {
128
                $purchaser = $this->getPurchaser(get_class($position), $position->getBatchPurchaseStrategyClass());
129
            } else {
130
                $purchaser = $this->getPurchaser(get_class($position), OneByOnePurchaseStrategy::class);
131
            }
132
133
            $purchaser->addPosition($position);
134
        }
135
    }
136
137
    /**
138
     * @param string $positionClass
139
     * @param string $purchaserClass
140
     * @return PurchaseStrategyInterface
141
     */
142
    protected function getPurchaser($positionClass, $purchaserClass)
143
    {
144
        if (!isset($this->purchasers[$positionClass])) {
145
            $this->purchasers[$positionClass] = new $purchaserClass($this->cart);
146
        }
147
148
        return $this->purchasers[$positionClass];
149
    }
150
}
151