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\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
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 |