Passed
Pull Request — master (#600)
by
unknown
02:53
created

Order::getOrderById()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2016 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Helper;
28
29
use Magento\Quote\Model\Quote\Address;
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Model\Quote\Address was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use Magento\Framework\Exception\LocalizedException;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Exception\LocalizedException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use Magento\Quote\Model\Quote;
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Model\Quote was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
use Magento\Sales\Model\Order as SalesOrder;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
34
/**
35
 * Helper class for everything that has to do with orders
36
 */
37
class Order extends \Payone\Core\Helper\Base
38
{
39
    /**
40
     * PAYONE database helper
41
     *
42
     * @var \Payone\Core\Helper\Database
43
     */
44
    protected $databaseHelper;
45
46
    /**
47
     * PAYONE customer helper
48
     *
49
     * @var \Payone\Core\Helper\Customer
50
     */
51
    protected $customerHelper;
52
53
    /**
54
     * Order factory
55
     *
56
     * @var \Magento\Sales\Model\OrderFactory
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\OrderFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
     */
58
    protected $orderFactory;
59
60
    /**
61
     * Totals collector object
62
     *
63
     * @var \Magento\Quote\Model\Quote\TotalsCollector
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Model\Quote\TotalsCollector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
64
     */
65
    protected $totalsCollector;
66
67
    /**
68
     * Constructor
69
     *
70
     * @param \Magento\Framework\App\Helper\Context      $context
71
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
72
     * @param \Payone\Core\Helper\Shop                   $shopHelper
73
     * @param \Magento\Framework\App\State               $state
74
     * @param \Payone\Core\Helper\Database               $databaseHelper
75
     * @param \Payone\Core\Helper\Customer               $customerHelper
76
     * @param \Magento\Sales\Model\OrderFactory          $orderFactory
77
     * @param \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector
78
     */
79
    public function __construct(
80
        \Magento\Framework\App\Helper\Context $context,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Helper\Context was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
        \Magento\Store\Model\StoreManagerInterface $storeManager,
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\StoreManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
82
        \Payone\Core\Helper\Shop $shopHelper,
83
        \Magento\Framework\App\State $state,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\State was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
84
        \Payone\Core\Helper\Database $databaseHelper,
85
        \Payone\Core\Helper\Customer $customerHelper,
86
        \Magento\Sales\Model\OrderFactory $orderFactory,
87
        \Magento\Quote\Model\Quote\TotalsCollector $totalsCollector
88
    ) {
89
        parent::__construct($context, $storeManager, $shopHelper, $state);
90
        $this->databaseHelper = $databaseHelper;
91
        $this->customerHelper = $customerHelper;
92
        $this->orderFactory = $orderFactory;
93
        $this->totalsCollector = $totalsCollector;
94
    }
95
96
    /**
97
     * Return the order related to the given TransactionStatus
98
     *
99
     * @param  string $sTxid
100
     * @return SalesOrder|null
101
     */
102
    public function getOrderByTxid($sTxid)
103
    {
104
        $sIncrementId = $this->databaseHelper->getOrderIncrementIdByTxid($sTxid);
105
        $oOrder = $this->orderFactory->create()->loadByIncrementId($sIncrementId);
106
        if ($oOrder && $oOrder->getId()) {
107
            return $oOrder;
108
        }
109
        return null;
110
    }
111
112
    /**
113
     * Return the order related to the given order id
114
     *
115
     * @param  string $sOrderId
116
     * @return SalesOrder|null
117
     */
118
    public function getOrderById($sOrderId)
119
    {
120
        $oOrder = $this->orderFactory->create()->load($sOrderId);
121
        if ($oOrder && $oOrder->getId()) {
122
            return $oOrder;
123
        }
124
        return null;
125
    }
126
127
    /**
128
     * Fill billing and shipping addresses with the needed information from the response
129
     *
130
     * @param  Address $oAddress
131
     * @param  string  $sFirstname
132
     * @param  string  $sLastname
133
     * @param  string  $sStreet
134
     * @param  string  $sCity
135
     * @param  string  $sZip
136
     * @param  string  $sCountry
137
     * @param  string  $sState
138
     * @return Address
139
     */
140
    public function fillSingleAddress(Address $oAddress, $sFirstname, $sLastname, $sStreet, $sCity, $sZip, $sCountry, $sState)
141
    {
142
        $oAddress->setFirstname($sFirstname);
143
        $oAddress->setLastname($sLastname);
144
        $oAddress->setStreet($sStreet);
145
        $oAddress->setCity($sCity);
146
        $oAddress->setPostcode($sZip);
147
        $oAddress->setCountryId($sCountry);
148
149
        $oRegion = $this->customerHelper->getRegion($sCountry, $sState);
150
        if ($oRegion) {
151
            $oAddress->setRegionId($oRegion->getId());
152
            $oAddress->setRegionCode($sState);
153
        } else {
154
            $oAddress->setRegionId(null);
155
            $oAddress->setRegionCode(null);
156
        }
157
        return $oAddress;
158
    }
159
160
    /**
161
     * Map response parameters to distinct parameters
162
     *
163
     * @param  Address $oAddress
164
     * @param  array   $aResponse
165
     * @param  bool    $blIsBilling
166
     * @return Address
167
     */
168
    public function fillSingleAddressByResponse($oAddress, $aResponse, $blIsBilling = false)
169
    {
170
        $sPrefix = 'shipping';
171
        if ($blIsBilling === true) {
172
            $sPrefix = 'billing';
173
        }
174
        return $this->fillSingleAddress(
175
            $oAddress,
176
            $aResponse['add_paydata['.$sPrefix.'_firstname]'],
177
            $aResponse['add_paydata['.$sPrefix.'_lastname]'],
178
            $aResponse['add_paydata['.$sPrefix.'_street]'],
179
            $aResponse['add_paydata['.$sPrefix.'_city]'],
180
            $aResponse['add_paydata['.$sPrefix.'_zip]'],
181
            $aResponse['add_paydata['.$sPrefix.'_country]'],
182
            $aResponse['add_paydata['.$sPrefix.'_country]']
183
        );
184
    }
185
186
    /**
187
     * Address-handling for billing- and shipping address
188
     *
189
     * @param  Quote $oQuote
190
     * @param  array $aResponse
191
     * @param  bool $blUseBilling
192
     * @return Quote
193
     */
194
    public function updateAddresses(Quote $oQuote, $aResponse, $blUseBilling = false)
195
    {
196
        $oBillingAddress = $this->fillSingleAddressByResponse($oQuote->getBillingAddress(), $aResponse, $blUseBilling);
197
        $oBillingAddress->setEmail($aResponse['add_paydata[email]']);
198
199
        $oQuote->setBillingAddress($oBillingAddress);
200
        $oQuote->getBillingAddress()->setShouldIgnoreValidation(true);
201
202
        if (!$oQuote->getIsVirtual()) {
203
            $oShippingAddress = $this->fillSingleAddressByResponse($oQuote->getShippingAddress(), $aResponse);
204
            #$oShippingAddress->setCollectShippingRates(true);
205
            $this->setShippingMethod($oShippingAddress, $oQuote);
206
            $oQuote->setShippingAddress($oShippingAddress);
207
            $oQuote->getShippingAddress()->setShouldIgnoreValidation(true);
208
        }
209
210
        return $oQuote;
211
    }
212
213
    /**
214
     * Determine the cheapest available shipping method
215
     *
216
     * @param  Quote   $oQuote
217
     * @param  Address $oShippingAddress
218
     * @return string|bool
219
     */
220
    public function getShippingMethod(Quote $oQuote, Address $oShippingAddress)
221
    {
222
        $aRates = [];
223
224
        // Needed for getGroupedAllShippingRates, otherwise sometimes empty output
225
        $this->totalsCollector->collectAddressTotals($oQuote, $oShippingAddress);
226
        $oShippingRates = $oShippingAddress->getGroupedAllShippingRates();
227
228
        foreach ($oShippingRates as $oCarrierRates) {
229
            foreach ($oCarrierRates as $oRate) {
230
                $aRates[(string)$oRate->getPrice()] = $oRate->getCode();
231
            }
232
        }
233
234
        if (!empty($aRates)) { // more than one shipping method existing?
235
            ksort($aRates); // sort by price ascending
236
            return array_shift($aRates); // return the cheapest shipping-method
237
        }
238
        return false;
239
    }
240
241
    /**
242
     * Get Shipping method and add it to the shipping-address object
243
     *
244
     * @param  Address $oAddress
245
     * @param  Quote   $oQuote
246
     * @return Address
247
     * @throws LocalizedException
248
     */
249
    public function setShippingMethod(Address $oAddress, Quote $oQuote)
250
    {
251
        $oAddress->setCollectShippingRates(true);
252
253
        $sShippingMethod = $this->getShippingMethod($oQuote, $oAddress);
254
        if (!$sShippingMethod) {
255
            throw new LocalizedException(__("No shipping method available for your address!"));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

255
            throw new LocalizedException(/** @scrutinizer ignore-call */ __("No shipping method available for your address!"));
Loading history...
256
        }
257
        $oAddress->setShippingMethod($sShippingMethod);
258
259
        return $oAddress;
260
    }
261
}
262