Completed
Push — master ( 9db63e...40913a )
by Oleksandr
24s queued 13s
created

PayoneSecurityInvoiceReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nc 1
nop 5
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\PayoneGetSecurityInvoiceTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...SecurityInvoiceTransfer 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\GetSecurityInvoiceResponseContainer;
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 PayoneSecurityInvoiceReader implements PayoneSecurityInvoiceReaderInterface
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\PayoneGetSecurityInvoiceTransfer $getSecurityInvoiceTransfer
72
     *
73
     * @return \Generated\Shared\Transfer\PayoneGetSecurityInvoiceTransfer
74
     */
75
    public function getSecurityInvoice(PayoneGetSecurityInvoiceTransfer $getSecurityInvoiceTransfer): PayoneGetSecurityInvoiceTransfer
76
    {
77
        $responseContainer = new GetSecurityInvoiceResponseContainer();
78
        $paymentEntity = $this->findPaymentByInvoiceTitleAndCustomerId(
79
            $getSecurityInvoiceTransfer->getReference(),
80
            $getSecurityInvoiceTransfer->getCustomerId()
81
        );
82
83
        if (!$paymentEntity) {
84
            return $this->setAccessDeniedError($getSecurityInvoiceTransfer);
85
        }
86
87
        /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\SecurityInvoice $paymentMethodMapper */
88
        $paymentMethodMapper = $this->paymentMapperReader->getRegisteredPaymentMethodMapper(PayoneApiConstants::PAYMENT_METHOD_SECURITY_INVOICE);
89
        /** @var \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer $requestContainer */
90
        $requestContainer = $paymentMethodMapper->mapGetSecurityInvoice($getSecurityInvoiceTransfer);
91
        $this->standartParameterMapper->setStandardParameter($requestContainer, $this->standardParameter);
92
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
93
        $responseContainer->init($rawResponse);
94
95
        $getSecurityInvoiceTransfer->setRawResponse($responseContainer->getRawResponse());
96
        $getSecurityInvoiceTransfer->setStatus($responseContainer->getStatus());
97
        $getSecurityInvoiceTransfer->setErrorCode($responseContainer->getErrorcode());
98
        $getSecurityInvoiceTransfer->setInternalErrorMessage($responseContainer->getErrormessage());
99
100
        return $getSecurityInvoiceTransfer;
101
    }
102
103
    /**
104
     * @param string $invoiceTitle
105
     * @param int $customerId
106
     *
107
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayone|null
108
     */
109
    protected function findPaymentByInvoiceTitleAndCustomerId(string $invoiceTitle, int $customerId): ?SpyPaymentPayone
110
    {
111
        return $this->queryContainer->createPaymentByInvoiceTitleAndCustomerIdQuery($invoiceTitle, $customerId)->findOne();
112
    }
113
114
    /**
115
     * @param \Generated\Shared\Transfer\PayoneGetSecurityInvoiceTransfer $getSecurityInvoiceTransfer
116
     *
117
     * @return \Generated\Shared\Transfer\PayoneGetSecurityInvoiceTransfer
118
     */
119
    protected function setAccessDeniedError(PayoneGetSecurityInvoiceTransfer $getSecurityInvoiceTransfer): PayoneGetSecurityInvoiceTransfer
120
    {
121
        $getSecurityInvoiceTransfer->setStatus(PayoneApiConstants::RESPONSE_TYPE_ERROR);
122
        $getSecurityInvoiceTransfer->setInternalErrorMessage(static::ERROR_ACCESS_DENIED_MESSAGE);
123
        $getSecurityInvoiceTransfer->setCustomerErrorMessage(static::ERROR_ACCESS_DENIED_MESSAGE);
124
125
        return $getSecurityInvoiceTransfer;
126
    }
127
}
128