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

PayoneFileReader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 37
c 0
b 0
f 0
dl 0
loc 124
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setAccessDeniedError() 0 7 1
A findPaymentByFileReferenceAndCustomerId() 0 3 1
A fetchFile() 0 12 1
A __construct() 0 12 1
A getFile() 0 20 2
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\PayoneGetFileTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PayoneGetFileTransfer 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\GetFileResponseContainer;
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 PayoneFileReader implements PayoneFileReaderInterface
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\Key\HmacGeneratorInterface|\SprykerEco\Zed\Payone\Business\Key\HashGenerator
41
     */
42
    protected $hashGenerator;
43
44
    /**
45
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface
46
     */
47
    protected $standartParameterMapper;
48
49
    /**
50
     * @var \SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface
51
     */
52
    protected $paymentMapperReader;
53
54
    /**
55
     * @param \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface $executionAdapter
56
     * @param \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface $queryContainer
57
     * @param \Generated\Shared\Transfer\PayoneStandardParameterTransfer $standardParameter
58
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface $standartParameterMapper
59
     * @param \SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface $paymentMapperReader
60
     */
61
    public function __construct(
62
        AdapterInterface $executionAdapter,
63
        PayoneQueryContainerInterface $queryContainer,
64
        PayoneStandardParameterTransfer $standardParameter,
65
        StandartParameterMapperInterface $standartParameterMapper,
66
        PaymentMapperReaderInterface $paymentMapperReader
67
    ) {
68
        $this->executionAdapter = $executionAdapter;
69
        $this->queryContainer = $queryContainer;
70
        $this->standardParameter = $standardParameter;
71
        $this->standartParameterMapper = $standartParameterMapper;
72
        $this->paymentMapperReader = $paymentMapperReader;
73
    }
74
75
    /**
76
     * @param \Generated\Shared\Transfer\PayoneGetFileTransfer $getFileTransfer
77
     *
78
     * @return \Generated\Shared\Transfer\PayoneGetFileTransfer
79
     */
80
    public function getFile(PayoneGetFileTransfer $getFileTransfer): PayoneGetFileTransfer
81
    {
82
        $paymentEntity = $this->findPaymentByFileReferenceAndCustomerId(
83
            $getFileTransfer->getReference(),
84
            $getFileTransfer->getCustomerId()
85
        );
86
87
        if (!$paymentEntity) {
88
            return $this->setAccessDeniedError($getFileTransfer);
89
        }
90
91
        $responseContainer = $this->fetchFile($getFileTransfer);
92
93
        $getFileTransfer->setRawResponse($responseContainer->getRawResponse());
94
        $getFileTransfer->setStatus($responseContainer->getStatus());
95
        $getFileTransfer->setErrorCode($responseContainer->getErrorcode());
96
        $getFileTransfer->setCustomerErrorMessage($responseContainer->getCustomermessage());
97
        $getFileTransfer->setInternalErrorMessage($responseContainer->getErrormessage());
98
99
        return $getFileTransfer;
100
    }
101
102
    /**
103
     * @param string $fileReference
104
     * @param int $customerId
105
     *
106
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayone|null
107
     */
108
    protected function findPaymentByFileReferenceAndCustomerId(string $fileReference, int $customerId): ?SpyPaymentPayone
109
    {
110
        return $this->queryContainer->createPaymentByFileReferenceAndCustomerIdQuery($fileReference, $customerId)->findOne();
111
    }
112
113
    /**
114
     * @param \Generated\Shared\Transfer\PayoneGetFileTransfer $getFileTransfer
115
     *
116
     * @return \Generated\Shared\Transfer\PayoneGetFileTransfer
117
     */
118
    protected function setAccessDeniedError(PayoneGetFileTransfer $getFileTransfer): PayoneGetFileTransfer
119
    {
120
        $getFileTransfer->setStatus(PayoneApiConstants::RESPONSE_TYPE_ERROR);
121
        $getFileTransfer->setInternalErrorMessage(static::ERROR_ACCESS_DENIED_MESSAGE);
122
        $getFileTransfer->setCustomerErrorMessage(static::ERROR_ACCESS_DENIED_MESSAGE);
123
124
        return $getFileTransfer;
125
    }
126
127
    /**
128
     * @param \Generated\Shared\Transfer\PayoneGetFileTransfer $getFileTransfer
129
     *
130
     * @return \SprykerEco\Zed\Payone\Business\Api\Response\Container\GetFileResponseContainer
131
     */
132
    protected function fetchFile(PayoneGetFileTransfer $getFileTransfer): GetFileResponseContainer
133
    {
134
        $responseContainer = new GetFileResponseContainer();
135
136
        /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\DirectDebit $paymentMethodMapper */
137
        $paymentMethodMapper = $this->paymentMapperReader->getRegisteredPaymentMethodMapper(PayoneApiConstants::PAYMENT_METHOD_DIRECT_DEBIT);
138
        $requestContainer = $paymentMethodMapper->mapGetFile($getFileTransfer);
139
        $this->standartParameterMapper->setStandardParameter($requestContainer, $this->standardParameter);
140
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
141
        $responseContainer->init($rawResponse);
142
143
        return $responseContainer;
144
    }
145
}
146