CrefoPayApiLogger   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 17
eloc 45
c 3
b 0
f 0
dl 0
loc 166
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessage() 0 13 4
A __construct() 0 3 1
A getCrefoPayOrderId() 0 11 2
A getResultCode() 0 13 4
A createPaymentCrefoPayApiLogTransfer() 0 14 1
A logApiCall() 0 13 1
A getSalt() 0 13 4
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\CrefoPayApi\Business\Logger;
9
10
use Generated\Shared\Transfer\CrefoPayApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayApiRequestTransfer 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\CrefoPayApiResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...oPayApiResponseTransfer 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 Generated\Shared\Transfer\PaymentCrefoPayApiLogTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...tCrefoPayApiLogTransfer 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 Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
14
use SprykerEco\Zed\CrefoPayApi\Business\Exception\InvalidRequestTypeException;
15
use SprykerEco\Zed\CrefoPayApi\Persistence\CrefoPayApiEntityManagerInterface;
16
17
class CrefoPayApiLogger implements CrefoPayApiLoggerInterface
18
{
19
    use TransactionTrait;
20
21
    /**
22
     * @var string
23
     */
24
    protected const GET_REQUEST_METHOD = 'get%sRequest';
25
26
    /**
27
     * @var string
28
     */
29
    protected const GET_RESPONSE_METHOD = 'get%sResponse';
30
31
    /**
32
     * @var string
33
     */
34
    protected const INVALID_REQUEST_TYPE_ERROR_MESSAGE = 'Request type "%s" is not supported';
35
36
    /**
37
     * @var \SprykerEco\Zed\CrefoPayApi\Persistence\CrefoPayApiEntityManagerInterface
38
     */
39
    protected $entityManager;
40
41
    /**
42
     * @param \SprykerEco\Zed\CrefoPayApi\Persistence\CrefoPayApiEntityManagerInterface $entityManager
43
     */
44
    public function __construct(CrefoPayApiEntityManagerInterface $entityManager)
45
    {
46
        $this->entityManager = $entityManager;
47
    }
48
49
    /**
50
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
51
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
52
     * @param string $requestType
53
     *
54
     * @return \Generated\Shared\Transfer\PaymentCrefoPayApiLogTransfer
55
     */
56
    public function logApiCall(
57
        CrefoPayApiRequestTransfer $requestTransfer,
58
        CrefoPayApiResponseTransfer $responseTransfer,
59
        string $requestType
60
    ): PaymentCrefoPayApiLogTransfer {
61
        $paymentCrefoPayApiLog = $this->createPaymentCrefoPayApiLogTransfer(
62
            $requestTransfer,
63
            $responseTransfer,
64
            $requestType,
65
        );
66
67
        return $this->getTransactionHandler()->handleTransaction(function () use ($paymentCrefoPayApiLog) {
68
            return $this->entityManager->savePaymentCrefoPayApiLog($paymentCrefoPayApiLog);
69
        });
70
    }
71
72
    /**
73
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
74
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
75
     * @param string $requestType
76
     *
77
     * @return \Generated\Shared\Transfer\PaymentCrefoPayApiLogTransfer
78
     */
79
    protected function createPaymentCrefoPayApiLogTransfer(
80
        CrefoPayApiRequestTransfer $requestTransfer,
81
        CrefoPayApiResponseTransfer $responseTransfer,
82
        string $requestType
83
    ): PaymentCrefoPayApiLogTransfer {
84
        return (new PaymentCrefoPayApiLogTransfer())
85
            ->setRequestType($requestType)
86
            ->setCrefoPayOrderId($this->getCrefoPayOrderId($requestTransfer, $requestType))
87
            ->setIsSuccess($responseTransfer->getIsSuccess())
88
            ->setResultCode($this->getResultCode($responseTransfer, $requestType))
89
            ->setMessage($this->getMessage($responseTransfer, $requestType))
90
            ->setSalt($this->getSalt($responseTransfer, $requestType))
91
            ->setRequest($requestTransfer->serialize())
92
            ->setResponse($responseTransfer->serialize());
93
    }
94
95
    /**
96
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
97
     * @param string $requestType
98
     *
99
     * @throws \SprykerEco\Zed\CrefoPayApi\Business\Exception\InvalidRequestTypeException
100
     *
101
     * @return string
102
     */
103
    protected function getCrefoPayOrderId(CrefoPayApiRequestTransfer $requestTransfer, string $requestType): string
104
    {
105
        $method = sprintf(static::GET_REQUEST_METHOD, ucfirst($requestType));
106
107
        if (!method_exists($requestTransfer, $method)) {
108
            throw new InvalidRequestTypeException(
109
                sprintf(static::INVALID_REQUEST_TYPE_ERROR_MESSAGE, $requestType),
110
            );
111
        }
112
113
        return $requestTransfer->$method()->getOrderID();
114
    }
115
116
    /**
117
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
118
     * @param string $requestType
119
     *
120
     * @throws \SprykerEco\Zed\CrefoPayApi\Business\Exception\InvalidRequestTypeException
121
     *
122
     * @return int|null
123
     */
124
    protected function getResultCode(CrefoPayApiResponseTransfer $responseTransfer, string $requestType): ?int
125
    {
126
        if ($responseTransfer->getIsSuccess() === false && $responseTransfer->getError() !== null) {
127
            return $responseTransfer->getError()->getResultCode();
128
        }
129
130
        $method = sprintf(static::GET_RESPONSE_METHOD, ucfirst($requestType));
131
132
        if (!method_exists($responseTransfer, $method)) {
133
            throw new InvalidRequestTypeException();
134
        }
135
136
        return $responseTransfer->$method()->getResultCode();
137
    }
138
139
    /**
140
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
141
     * @param string $requestType
142
     *
143
     * @throws \SprykerEco\Zed\CrefoPayApi\Business\Exception\InvalidRequestTypeException
144
     *
145
     * @return string|null
146
     */
147
    protected function getMessage(CrefoPayApiResponseTransfer $responseTransfer, string $requestType): ?string
148
    {
149
        if ($responseTransfer->getIsSuccess() === false && $responseTransfer->getError() !== null) {
150
            return $responseTransfer->getError()->getMessage();
151
        }
152
153
        $method = sprintf(static::GET_RESPONSE_METHOD, ucfirst($requestType));
154
155
        if (!method_exists($responseTransfer, $method)) {
156
            throw new InvalidRequestTypeException();
157
        }
158
159
        return $responseTransfer->$method()->getMessage();
160
    }
161
162
    /**
163
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
164
     * @param string $requestType
165
     *
166
     * @throws \SprykerEco\Zed\CrefoPayApi\Business\Exception\InvalidRequestTypeException
167
     *
168
     * @return string|null
169
     */
170
    protected function getSalt(CrefoPayApiResponseTransfer $responseTransfer, string $requestType): ?string
171
    {
172
        if ($responseTransfer->getIsSuccess() === false && $responseTransfer->getError() !== null) {
173
            return $responseTransfer->getError()->getSalt();
174
        }
175
176
        $method = sprintf(static::GET_RESPONSE_METHOD, ucfirst($requestType));
177
178
        if (!method_exists($responseTransfer, $method)) {
179
            throw new InvalidRequestTypeException();
180
        }
181
182
        return $responseTransfer->$method()->getSalt();
183
    }
184
}
185