Completed
Push — master ( 1b1f21...9e6ce8 )
by Florian
11s
created

Appointed::execute()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 6.7272
cc 7
eloc 18
nc 7
nop 1
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;
30
use Magento\Sales\Model\Order\Invoice;
31
use Magento\Framework\Event\ObserverInterface;
32
use Magento\Framework\Event\Observer;
33
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
34
use Psr\Log\LoggerInterface;
35
use Magento\Sales\Model\Service\InvoiceService;
36
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
37
use Payone\Core\Helper\Base;
38
use Payone\Core\Model\PayoneConfig;
39
40
/**
41
 * Event observer for Transactionstatus appointed
42
 */
43
class Appointed implements ObserverInterface
44
{
45
    /**
46
     * Logger object
47
     *
48
     * @var LoggerInterface
49
     */
50
    protected $logger = null;
51
52
    /**
53
     * OrderSender object
54
     *
55
     * @var OrderSender
56
     */
57
    protected $orderSender = null;
58
59
    /**
60
     * InvoiceService object
61
     *
62
     * @var InvoiceService
63
     */
64
    protected $invoiceService;
65
66
    /**
67
     * InvoiceSender object
68
     *
69
     * @var InvoiceSender
70
     */
71
    protected $invoiceSender;
72
73
    /**
74
     * Payone base helper
75
     *
76
     * @var Base
77
     */
78
    protected $baseHelper;
79
80
    /**
81
     * Constructor.
82
     *
83
     * @param LoggerInterface $logger
84
     * @param OrderSender     $orderSender
85
     * @param InvoiceService  $invoiceService
86
     * @param InvoiceSender   $invoiceSender
87
     * @param Base            $baseHelper
88
     */
89
    public function __construct(
90
        LoggerInterface $logger,
91
        OrderSender $orderSender,
92
        InvoiceService $invoiceService,
93
        InvoiceSender $invoiceSender,
94
        Base $baseHelper
95
    ) {
96
        $this->logger = $logger;
97
        $this->orderSender = $orderSender;
98
        $this->invoiceService = $invoiceService;
99
        $this->invoiceSender = $invoiceSender;
100
        $this->baseHelper = $baseHelper;
101
    }
102
103
    /**
104
     * Generate an invoice for the order to mark the order as paid
105
     *
106
     * @param  Observer $observer
107
     * @return void
108
     */
109
    public function execute(Observer $observer)
110
    {
111
        /* @var $oOrder Order */
112
        $oOrder = $observer->getOrder();
113
114
        // order is not guaranteed to exist if using transaction status forwarding
115
        if (null === $oOrder || $oOrder->getEmailSent()) {
116
            return;
117
        }
118
119
        try {
120
            $this->orderSender->send($oOrder);
121
        } catch (\Exception $e) {
122
            $this->logger->critical($e);
123
        }
124
125
        // preauthorization-orders and advance payment should not create an invoice
126
        if ($oOrder->getPayoneAuthmode() != 'authorization' || $oOrder->getPayment()->getMethodInstance()->getCode() == PayoneConfig::METHOD_ADVANCE_PAYMENT){
127
            return;
128
        }
129
130
        $oInvoice = $this->invoiceService->prepareInvoice($oOrder);
131
        $oInvoice->setRequestedCaptureCase(Invoice::NOT_CAPTURE);
132
        $oInvoice->setTransactionId($oOrder->getPayment()->getLastTransId());
133
        $oInvoice->register();
134
        $oInvoice->save();
135
136
        $oOrder->save();
137
138
        if ($this->baseHelper->getConfigParam('send_invoice_email', 'emails')) {
139
            $this->invoiceSender->send($oInvoice);
140
        }
141
    }
142
}
143