Issues (1092)

Model/Plugins/GenerateGiftCardAccountsInvoice.php (5 issues)

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * PHP version 5
19
 *
20
 * @category  Payone
21
 * @package   Payone_Magento2_Plugin
22
 * @author    run_as_root GmbH <[email protected]>
23
 * @copyright 2003 - 2020 Payone GmbH
24
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
25
 * @link      http://www.payone.de
26
 */
27
28
namespace Payone\Core\Model\Plugins;
29
30
use Magento\Framework\Event\Observer;
0 ignored issues
show
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...
31
use Magento\Framework\Exception\LocalizedException;
0 ignored issues
show
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...
32
use Magento\GiftCard\Observer\GenerateGiftCardAccountsInvoice as GenerateGiftCardAccountsInvoiceOriginal;
0 ignored issues
show
The type Magento\GiftCard\Observe...GiftCardAccountsInvoice 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\Sales\Model\Order\Invoice;
0 ignored issues
show
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...
34
use Payone\Core\Model\Methods\PayoneMethod;
35
use Payone\Core\Model\PayoneConfig;
36
37
class GenerateGiftCardAccountsInvoice
38
{
39
    /**
40
     *
41
     * PayOne saves the invoices multiple times (appointed and paid).
42
     * Magento creates a gift card account everytime the invoice is saved.
43
     *
44
     * Only proceed with original Observer if invoice is paid
45
     * to avoid duplicate gift card accounts
46
     *
47
     * @param GenerateGiftCardAccountsInvoiceOriginal $subject
48
     * @param callable                                $proceed
49
     * @param Observer                                $observer
50
     *
51
     * @return mixed
52
     */
53
54
    public function aroundExecute(GenerateGiftCardAccountsInvoiceOriginal $subject, callable $proceed, $observer)
0 ignored issues
show
The parameter $subject is not used and could be removed. ( Ignorable by Annotation )

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

54
    public function aroundExecute(/** @scrutinizer ignore-unused */ GenerateGiftCardAccountsInvoiceOriginal $subject, callable $proceed, $observer)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        /** @var Invoice $invoice */
57
        $invoice               = $observer->getInvoice();
58
        $paymentMethodInstance = $invoice->getOrder()->getPayment()->getMethodInstance();
59
60
        if ($paymentMethodInstance instanceof PayoneMethod) {
61
            try {
62
                $paymentMethodCode = $paymentMethodInstance->getCode();
63
64
                if ($this->isAdvancedPaymentAndInvoiceIsOpen($paymentMethodCode, $invoice) || $this->isAnyOtherPaymentAndInvoiceIsPaid($paymentMethodCode, $invoice)) {
65
                    return NULL;
66
                }
67
            } catch (LocalizedException $exception) {
68
                // continue with regular plugin flow
69
            }
70
        }
71
72
        return $proceed($observer);
73
    }
74
75
    private function isAdvancedPaymentAndInvoiceIsOpen(string $paymentMethodCode, Invoice $invoice)
76
    {
77
        return $paymentMethodCode === PayoneConfig::METHOD_ADVANCE_PAYMENT && $invoice->getState() === Invoice::STATE_OPEN;
78
    }
79
80
    private function isAnyOtherPaymentAndInvoiceIsPaid(string $paymentMethodCode, Invoice $invoice)
81
    {
82
        return $paymentMethodCode !== PayoneConfig::METHOD_ADVANCE_PAYMENT && $invoice->getState() === Invoice::STATE_PAID;
83
    }
84
}
85