OneByOnePurchaseStrategy   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 65
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addPosition() 0 4 1
A run() 0 8 2
A purchase() 0 20 5
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\hiart\ResponseErrorException;
14
use hiqdev\yii2\cart\ShoppingCart;
15
16
/**
17
 * Class OneByOnePurchaseStrategy is used to purchase positions one-by-one.
18
 *
19
 * @author Dmytro Naumenko <[email protected]>
20
 */
21
class OneByOnePurchaseStrategy implements PurchaseStrategyInterface
22
{
23
    use PurchaseResultTrait;
24
25
    /**
26
     * @var ShoppingCart
27
     */
28
    protected $cart;
29
30
    /**
31
     * @var AbstractCartPosition[]
32
     */
33
    protected $positions = [];
34
35
    /**
36
     * OneByOnePurchaseStrategy constructor.
37
     *
38
     * @param ShoppingCart $cart
39
     */
40
    public function __construct(ShoppingCart $cart)
41
    {
42
        $this->cart = $cart;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function addPosition(AbstractCartPosition $position)
49
    {
50
        $this->positions[$position->getId()] = $position;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function run()
57
    {
58
        $this->resetPurchaseResults();
59
60
        foreach ($this->positions as $position) {
61
            $this->purchase($position);
62
        }
63
    }
64
65
    protected function purchase(AbstractCartPosition $position)
66
    {
67
        $purchase = $position->getPurchaseModel();
68
69
        try {
70
            if ($purchase->execute()) {
71
                $this->success[] = $purchase;
72
73
                return;
74
            }
75
76
            $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...
77
        } catch (PendingPurchaseException $e) {
78
            $this->pending[] = $e;
79
        } catch (ResponseErrorException $e) {
80
            $this->error[] = new ErrorPurchaseException($e->getMessage(), $purchase, $e);
81
        } catch (\hiqdev\hiart\Exception $e) {
82
            $this->error[] = new ErrorPurchaseException($e->getMessage(), $purchase, $e);
83
        }
84
    }
85
}
86