Completed
Push — master ( 341936...178071 )
by Florian
03:31
created

Capture   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 11.21 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 12
loc 107
c 0
b 0
f 0
wmc 11
lcom 1
cbo 6
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 1
C getInvoiceList() 0 23 8
B sendRequest() 0 31 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Model\Api\Request;
28
29
use Magento\Payment\Model\InfoInterface;
30
use Payone\Core\Model\Methods\PayoneMethod;
31
use Magento\Sales\Model\Order;
32
33
/**
34
 * Class for the PAYONE Server API request "capture"
35
 */
36
class Capture extends Base
37
{
38
    /**
39
     * @var \Payone\Core\Model\Api\Invoice $invoiceGenerator
40
     */
41
    protected $invoiceGenerator;
42
43
    /**
44
     * PAYONE database helper
45
     *
46
     * @var \Payone\Core\Helper\Database
47
     */
48
    protected $databaseHelper;
49
50
    /**
51
     * Constructor
52
     *
53
     * @param \Payone\Core\Helper\Shop                $shopHelper
54
     * @param \Payone\Core\Helper\Environment         $environmentHelper
55
     * @param \Payone\Core\Helper\Api                 $apiHelper
56
     * @param \Payone\Core\Model\ResourceModel\ApiLog $apiLog
57
     * @param \Payone\Core\Model\Api\Invoice          $invoiceGenerator
58
     * @param \Payone\Core\Helper\Database            $databaseHelper
59
     */
60 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
        \Payone\Core\Helper\Shop $shopHelper,
62
        \Payone\Core\Helper\Environment $environmentHelper,
63
        \Payone\Core\Helper\Api $apiHelper,
64
        \Payone\Core\Model\ResourceModel\ApiLog $apiLog,
65
        \Payone\Core\Model\Api\Invoice $invoiceGenerator,
66
        \Payone\Core\Helper\Database $databaseHelper
67
    ) {
68
        parent::__construct($shopHelper, $environmentHelper, $apiHelper, $apiLog);
69
        $this->invoiceGenerator = $invoiceGenerator;
70
        $this->databaseHelper = $databaseHelper;
71
    }
72
73
    /**
74
     * Generate position list for invoice data transmission
75
     *
76
     * @param Order $oOrder
77
     * @return array|false
78
     */
79
    protected function getInvoiceList(Order $oOrder)
80
    {
81
        $aInvoice = $this->shopHelper->getRequestParameter('invoice');
82
83
        $aPositions = [];
84
        $blFull = true;
85
        if ($aInvoice && array_key_exists('items', $aInvoice) !== false) {
86
            foreach ($oOrder->getAllItems() as $oItem) {
87
                if (isset($aInvoice['items'][$oItem->getItemId()]) && $aInvoice['items'][$oItem->getItemId()] > 0) {
88
                    $aPositions[$oItem->getProductId()] = $aInvoice['items'][$oItem->getItemId()];
89
                    if ($aInvoice['items'][$oItem->getItemId()] != $oItem->getQtyOrdered()) {
90
                        $blFull = false;
91
                    }
92
                } else {
93
                    $blFull = false;
94
                }
95
            }
96
        }
97
        if ($blFull === true) {
98
            $aPositions = false;
99
        }
100
        return $aPositions;
101
    }
102
103
    /**
104
     * Send request "capture" to PAYONE server API
105
     *
106
     * @param  PayoneMethod  $oPayment
107
     * @param  InfoInterface $oPaymentInfo
108
     * @param  float         $dAmount
109
     * @return array
110
     */
111
    public function sendRequest(PayoneMethod $oPayment, InfoInterface $oPaymentInfo, $dAmount)
112
    {
113
        $oOrder = $oPaymentInfo->getOrder();
114
115
        $aPositions = $this->getInvoiceList($oOrder);
116
117
        $iTxid = $oPaymentInfo->getParentTransactionId();
118
119
        $this->setOrderId($oOrder->getRealOrderId());
120
121
        $this->addParameter('request', 'capture'); // Request method
122
        $this->addParameter('mode', $oPayment->getOperationMode()); // PayOne Portal Operation Mode (live or test)
123
        $this->addParameter('language', $this->shopHelper->getLocale());
124
125
        // Total order sum in smallest currency unit
126
        $this->addParameter('amount', number_format($dAmount, 2, '.', '') * 100);
127
        $this->addParameter('currency', $oOrder->getOrderCurrencyCode()); // Currency
128
129
        $this->addParameter('txid', $iTxid); // PayOne Transaction ID
130
        $this->addParameter('sequencenumber', $this->databaseHelper->getSequenceNumber($iTxid));
131
132
        $this->addParameter('settleaccount', 'auto');
133
134
        if ($this->apiHelper->isInvoiceDataNeeded($oPayment)) {
135
            $this->invoiceGenerator->addProductInfo($this, $oOrder, $aPositions); // add invoice parameters
136
        }
137
138
        $aResponse = $this->send();
139
140
        return $aResponse;
141
    }
142
}
143