1 | <?php |
||
2 | /** |
||
3 | * Copyright © Wirecard Brasil. All rights reserved. |
||
4 | * |
||
5 | * @author Bruno Elisei <[email protected]> |
||
6 | * See COPYING.txt for license details. |
||
7 | */ |
||
8 | |||
9 | declare(strict_types=1); |
||
10 | |||
11 | namespace Moip\Magento2\Gateway; |
||
12 | |||
13 | use Magento\Checkout\Model\Session; |
||
14 | use Magento\Payment\Gateway\Data\PaymentDataObjectInterface; |
||
15 | use Magento\Payment\Gateway\Helper; |
||
16 | |||
17 | /** |
||
18 | * Class SubjectReader - Reading data. |
||
19 | */ |
||
20 | class SubjectReader |
||
21 | { |
||
22 | /** |
||
23 | * @var Checkout Session |
||
24 | */ |
||
25 | private $checkoutSession; |
||
26 | |||
27 | /** |
||
28 | * SubjectReader constructor. |
||
29 | * |
||
30 | * @param Session $checkoutSession |
||
31 | */ |
||
32 | public function __construct( |
||
33 | Session $checkoutSession |
||
34 | ) { |
||
35 | $this->checkoutSession = $checkoutSession; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Reads payment from subject. |
||
40 | * |
||
41 | * @param array $subject |
||
42 | * |
||
43 | * @return PaymentDataObjectInterface |
||
44 | */ |
||
45 | public function readPayment(array $subject): PaymentDataObjectInterface |
||
46 | { |
||
47 | return Helper\SubjectReader::readPayment($subject); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Reads store's ID, otherwise returns null. |
||
52 | * |
||
53 | * @param array $subject |
||
54 | * |
||
55 | * @return int|null |
||
56 | */ |
||
57 | public function readStoreId(array $subject): int |
||
58 | { |
||
59 | $storeId = $subject['store_id'] ?? null; |
||
60 | |||
61 | if (empty($storeId)) { |
||
62 | try { |
||
63 | $storeId = (int) $this->readPayment($subject) |
||
64 | ->getOrder() |
||
65 | ->getStoreId(); |
||
66 | // phpcs:ignore Magento2.CodeAnalysis.EmptyBlock |
||
67 | } catch (\InvalidArgumentException $e) { |
||
68 | // No store id is current set |
||
69 | } |
||
70 | } |
||
71 | |||
72 | return $storeId ? (int) $storeId : null; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Reads amount from subject. |
||
77 | * |
||
78 | * @param array $subject |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function readAmount(array $subject): string |
||
83 | { |
||
84 | return (string) Helper\SubjectReader::readAmount($subject); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Reads response from subject. |
||
89 | * |
||
90 | * @param array $subject |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | public function readResponse(array $subject): array |
||
95 | { |
||
96 | return Helper\SubjectReader::readResponse($subject); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @return \Magento\Quote\Model\Quote |
||
101 | */ |
||
102 | public function getQuote() |
||
103 | { |
||
104 | return $this->checkoutSession->getQuote(); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return \Magento\Sales\Model\Order |
||
109 | */ |
||
110 | public function getOrder() |
||
111 | { |
||
112 | return $this->checkoutSession->getLastRealOrder(); |
||
113 | } |
||
114 | } |
||
115 |