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

Paid::execute()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
cc 7
eloc 22
nc 17
nop 1
dl 0
loc 39
rs 8.6346
c 4
b 2
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\Observer\Transactionstatus;
28
29
use Magento\Sales\Model\Order;
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...
30
use Magento\Sales\Model\Order\Invoice;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order\Invoice 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\Sales\Model\Service\InvoiceService;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Service\InvoiceService 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\Email\Sender\InvoiceSender;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Orde...il\Sender\InvoiceSender 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
use Magento\Framework\Event\ObserverInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Event\ObserverInterface 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...
34
use Magento\Framework\Event\Observer;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Event\Observer 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...
35
use Payone\Core\Helper\Order as OrderHelper;
36
use Payone\Core\Model\PayoneConfig;
37
use Magento\Framework\App\CacheInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\CacheInterface 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...
38
39
/**
40
 * Event observer for Transactionstatus paid
41
 */
42
class Paid implements ObserverInterface
43
{
44
    /**
45
     * InvoiceService object
46
     *
47
     * @var InvoiceService
48
     */
49
    protected $invoiceService;
50
51
    /**
52
     * InvoiceSender object
53
     *
54
     * @var InvoiceSender
55
     */
56
    protected $invoiceSender;
57
58
    /**
59
     * PAYONE order helper
60
     *
61
     * @var OrderHelper
62
     */
63
    protected $orderHelper;
64
65
    /**
66
     * CacheInterface object
67
     *
68
     * @var CacheInterface
69
     */
70
    protected $cache;
71
72
    /**
73
     * Constructor.
74
     *
75
     * @param InvoiceService $invoiceService
76
     * @param InvoiceSender  $invoiceSender
77
     * @param OrderHelper    $orderHelper
78
     * @param CacheInterface $cache
79
     */
80
    public function __construct(
81
        InvoiceService $invoiceService,
82
        InvoiceSender  $invoiceSender,
83
        OrderHelper    $orderHelper,
84
        CacheInterface $cache
85
    ) {
86
        $this->invoiceService = $invoiceService;
87
        $this->invoiceSender  = $invoiceSender;
88
        $this->orderHelper    = $orderHelper;
89
        $this->cache          = $cache;
90
    }
91
92
    /**
93
     * Waits till the appointed lock is removed
94
     *
95
     * @param string $sLockKey
96
     * @return void
97
     */
98
    protected function waitTillAppointedIsFinished($sLockKey)
99
    {
100
        $iTrys = 10;
101
        while ($iTrys > 0) {
102
            sleep(5);
103
            $iTrys--;
104
            if (!$this->cache->load($sLockKey)) {
105
                break;
106
            }
107
        }
108
    }
109
110
    /**
111
     * Generate an invoice for the order to mark the order as paid
112
     *
113
     * @param Observer $observer
114
     *
115
     * @return void
116
     */
117
    public function execute(Observer $observer)
118
    {
119
        /* @var $oOrder Order */
120
        $oOrder = $observer->getOrder();
121
122
        // order is not guaranteed to exist if using transaction status forwarding
123
        if (NULL === $oOrder) {
124
            return;
125
        }
126
127
        $sAppointedLockKey = Appointed::STATUS_LOCK_IDENT.$oOrder->getId();
128
        if ($this->cache->load($sAppointedLockKey)) { // Appointed status is being processed at the moment
129
            $this->waitTillAppointedIsFinished($sAppointedLockKey);
130
131
            $oOrder = $this->orderHelper->getOrderById($oOrder->getId()); // Reload order because it will have changed in the meantime
132
        }
133
134
        // if advance payment is paid should create an invoice
135
        if ($oOrder->getPayment()->getMethodInstance()->getCode() === PayoneConfig::METHOD_ADVANCE_PAYMENT) {
136
            if ($this->orderHelper->getConfigParam('create_invoice', 'payone_advance_payment', 'payone_payment')) {
137
                $oInvoice = $this->invoiceService->prepareInvoice($oOrder);
138
                $oInvoice->setRequestedCaptureCase(Invoice::NOT_CAPTURE);
139
                $oInvoice->setTransactionId($oOrder->getPayment()->getLastTransId());
140
                $oInvoice->register();
141
                $oInvoice->save();
142
143
                $oOrder->save();
144
145
                if ($this->orderHelper->getConfigParam('send_invoice_email', 'emails')) {
146
                    $this->invoiceSender->send($oInvoice);
147
                }
148
            }
149
        }
150
151
        $aInvoiceList = $oOrder->getInvoiceCollection()->getItems();
152
        if ($oInvoice = array_shift($aInvoiceList)) { // get first invoice
153
            $oInvoice->pay(); // mark invoice as paid
154
            $oInvoice->save();
155
            $oOrder->save();
156
        }
157
    }
158
}
159