PayoneInvoiceReader::setAccessDeniedError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Payone\Business\Payment\Reader;
9
10
use Generated\Shared\Transfer\PayoneGetInvoiceTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PayoneGetInvoiceTransfer 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...
11
use Generated\Shared\Transfer\PayoneStandardParameterTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...andardParameterTransfer 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...
12
use Orm\Zed\Payone\Persistence\SpyPaymentPayone;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Payone\Persistence\SpyPaymentPayone 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...
13
use SprykerEco\Shared\Payone\PayoneApiConstants;
14
use SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface;
15
use SprykerEco\Zed\Payone\Business\Api\Response\Container\GetInvoiceResponseContainer;
16
use SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface;
17
use SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface;
18
use SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface;
19
20
class PayoneInvoiceReader implements PayoneInvoiceReaderInterface
21
{
22
    protected const ERROR_ACCESS_DENIED_MESSAGE = 'Access denied';
23
24
    /**
25
     * @var \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface
26
     */
27
    protected $executionAdapter;
28
29
    /**
30
     * @var \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface
31
     */
32
    protected $queryContainer;
33
34
    /**
35
     * @var \Generated\Shared\Transfer\PayoneStandardParameterTransfer
36
     */
37
    protected $standardParameter;
38
39
    /**
40
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface
41
     */
42
    protected $standartParameterMapper;
43
44
    /**
45
     * @var \SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface
46
     */
47
    protected $paymentMapperReader;
48
49
    /**
50
     * @param \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface $executionAdapter
51
     * @param \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface $queryContainer
52
     * @param \Generated\Shared\Transfer\PayoneStandardParameterTransfer $standardParameter
53
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface $standartParameterMapper
54
     * @param \SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface $paymentMapperReader
55
     */
56
    public function __construct(
57
        AdapterInterface $executionAdapter,
58
        PayoneQueryContainerInterface $queryContainer,
59
        PayoneStandardParameterTransfer $standardParameter,
60
        StandartParameterMapperInterface $standartParameterMapper,
61
        PaymentMapperReaderInterface $paymentMapperReader
62
    ) {
63
        $this->executionAdapter = $executionAdapter;
64
        $this->queryContainer = $queryContainer;
65
        $this->standardParameter = $standardParameter;
66
        $this->standartParameterMapper = $standartParameterMapper;
67
        $this->paymentMapperReader = $paymentMapperReader;
68
    }
69
70
    /**
71
     * @param \Generated\Shared\Transfer\PayoneGetInvoiceTransfer $getInvoiceTransfer
72
     *
73
     * @return \Generated\Shared\Transfer\PayoneGetInvoiceTransfer
74
     */
75
    public function getInvoice(PayoneGetInvoiceTransfer $getInvoiceTransfer): PayoneGetInvoiceTransfer
76
    {
77
        $paymentEntity = $this->findPaymentByInvoiceTitleAndCustomerId(
78
            $getInvoiceTransfer->getReference(),
79
            $getInvoiceTransfer->getCustomerId()
80
        );
81
82
        if (!$paymentEntity) {
83
            return $this->setAccessDeniedError($getInvoiceTransfer);
84
        }
85
86
        $responseContainer = $this->fetchInvoice($getInvoiceTransfer);
87
88
        $getInvoiceTransfer->setRawResponse($responseContainer->getRawResponse());
89
        $getInvoiceTransfer->setStatus($responseContainer->getStatus());
90
        $getInvoiceTransfer->setErrorCode($responseContainer->getErrorcode());
91
        $getInvoiceTransfer->setInternalErrorMessage($responseContainer->getErrormessage());
92
93
        return $getInvoiceTransfer;
94
    }
95
96
    /**
97
     * @param string $invoiceTitle
98
     * @param int $customerId
99
     *
100
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayone|null
101
     */
102
    protected function findPaymentByInvoiceTitleAndCustomerId(string $invoiceTitle, int $customerId): ?SpyPaymentPayone
103
    {
104
        return $this->queryContainer->createPaymentByInvoiceTitleAndCustomerIdQuery($invoiceTitle, $customerId)->findOne();
105
    }
106
107
    /**
108
     * @param \Generated\Shared\Transfer\PayoneGetInvoiceTransfer $getInvoiceTransfer
109
     *
110
     * @return \Generated\Shared\Transfer\PayoneGetInvoiceTransfer
111
     */
112
    protected function setAccessDeniedError(PayoneGetInvoiceTransfer $getInvoiceTransfer): PayoneGetInvoiceTransfer
113
    {
114
        $getInvoiceTransfer->setStatus(PayoneApiConstants::RESPONSE_TYPE_ERROR);
115
        $getInvoiceTransfer->setInternalErrorMessage(static::ERROR_ACCESS_DENIED_MESSAGE);
116
        $getInvoiceTransfer->setCustomerErrorMessage(static::ERROR_ACCESS_DENIED_MESSAGE);
117
118
        return $getInvoiceTransfer;
119
    }
120
121
    /**
122
     * @param \Generated\Shared\Transfer\PayoneGetInvoiceTransfer $getInvoiceTransfer
123
     *
124
     * @return \SprykerEco\Zed\Payone\Business\Api\Response\Container\GetInvoiceResponseContainer
125
     */
126
    protected function fetchInvoice(PayoneGetInvoiceTransfer $getInvoiceTransfer): GetInvoiceResponseContainer
127
    {
128
        $responseContainer = new GetInvoiceResponseContainer();
129
130
        /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\Invoice $paymentMethodMapper */
131
        $paymentMethodMapper = $this->paymentMapperReader->getRegisteredPaymentMethodMapper(PayoneApiConstants::PAYMENT_METHOD_INVOICE);
132
        $requestContainer = $paymentMethodMapper->mapGetInvoice($getInvoiceTransfer);
133
        $this->standartParameterMapper->setStandardParameter($requestContainer, $this->standardParameter);
134
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
135
        $responseContainer->init($rawResponse);
136
137
        return $responseContainer;
138
    }
139
}
140