Completed
Pull Request — master (#3)
by Aleksey
15:31 queued 12:58
created

createPaymentCrefoPayApiLogTransfer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 14
rs 9.9666
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
    protected const GET_REQUEST_METHOD = 'get%sRequest';
22
    protected const GET_RESPONSE_METHOD = 'get%sResponse';
23
    protected const INVALID_REQUEST_TYPE_ERROR_MESSAGE = 'Request type "%s" is not supported';
24
25
    /**
26
     * @var \SprykerEco\Zed\CrefoPayApi\Persistence\CrefoPayApiEntityManagerInterface
27
     */
28
    protected $entityManager;
29
30
    /**
31
     * @param \SprykerEco\Zed\CrefoPayApi\Persistence\CrefoPayApiEntityManagerInterface $entityManager
32
     */
33
    public function __construct(CrefoPayApiEntityManagerInterface $entityManager)
34
    {
35
        $this->entityManager = $entityManager;
36
    }
37
38
    /**
39
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
40
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
41
     * @param string $requestType
42
     *
43
     * @return \Generated\Shared\Transfer\PaymentCrefoPayApiLogTransfer
44
     */
45
    public function logApiCall(
46
        CrefoPayApiRequestTransfer $requestTransfer,
47
        CrefoPayApiResponseTransfer $responseTransfer,
48
        string $requestType
49
    ): PaymentCrefoPayApiLogTransfer {
50
        $paymentCrefoPayApiLog = $this->createPaymentCrefoPayApiLogTransfer(
51
            $requestTransfer,
52
            $responseTransfer,
53
            $requestType
54
        );
55
56
        return $this->getTransactionHandler()->handleTransaction(function () use ($paymentCrefoPayApiLog) {
57
            return $this->entityManager->savePaymentCrefoPayApiLog($paymentCrefoPayApiLog);
58
        });
59
    }
60
61
    /**
62
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
63
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
64
     * @param string $requestType
65
     *
66
     * @return \Generated\Shared\Transfer\PaymentCrefoPayApiLogTransfer
67
     */
68
    protected function createPaymentCrefoPayApiLogTransfer(
69
        CrefoPayApiRequestTransfer $requestTransfer,
70
        CrefoPayApiResponseTransfer $responseTransfer,
71
        string $requestType
72
    ): PaymentCrefoPayApiLogTransfer {
73
        return (new PaymentCrefoPayApiLogTransfer())
74
            ->setRequestType($requestType)
75
            ->setCrefoPayOrderId($this->getCrefoPayOrderId($requestTransfer, $requestType))
76
            ->setIsSuccess($responseTransfer->getIsSuccess())
77
            ->setResultCode($this->getResultCode($responseTransfer, $requestType))
78
            ->setMessage($this->getMessage($responseTransfer, $requestType))
79
            ->setSalt($this->getSalt($responseTransfer, $requestType))
80
            ->setRequest($requestTransfer->serialize())
81
            ->setResponse($responseTransfer->serialize());
82
    }
83
84
    /**
85
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
86
     * @param string $requestType
87
     *
88
     * @throws \SprykerEco\Zed\CrefoPayApi\Business\Exception\InvalidRequestTypeException
89
     *
90
     * @return string
91
     */
92
    protected function getCrefoPayOrderId(CrefoPayApiRequestTransfer $requestTransfer, string $requestType): string
93
    {
94
        $method = sprintf(static::GET_REQUEST_METHOD, ucfirst($requestType));
95
96
        if (!method_exists($requestTransfer, $method)) {
97
            throw new InvalidRequestTypeException(
98
                sprintf(static::INVALID_REQUEST_TYPE_ERROR_MESSAGE, $requestType)
99
            );
100
        }
101
102
        return $requestTransfer->$method()->getOrderID();
103
    }
104
105
    /**
106
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
107
     * @param string $requestType
108
     *
109
     * @throws \SprykerEco\Zed\CrefoPayApi\Business\Exception\InvalidRequestTypeException
110
     *
111
     * @return int|null
112
     */
113
    protected function getResultCode(CrefoPayApiResponseTransfer $responseTransfer, string $requestType): ?int
114
    {
115
        if ($responseTransfer->getIsSuccess() === false && $responseTransfer->getError() !== null) {
116
            return $responseTransfer->getError()->getResultCode();
117
        }
118
119
        $method = sprintf(static::GET_RESPONSE_METHOD, ucfirst($requestType));
120
121
        if (!method_exists($responseTransfer, $method)) {
122
            throw new InvalidRequestTypeException();
123
        }
124
125
        return $responseTransfer->$method()->getResultCode();
126
    }
127
128
    /**
129
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
130
     * @param string $requestType
131
     *
132
     * @throws \SprykerEco\Zed\CrefoPayApi\Business\Exception\InvalidRequestTypeException
133
     *
134
     * @return string|null
135
     */
136
    protected function getMessage(CrefoPayApiResponseTransfer $responseTransfer, string $requestType): ?string
137
    {
138
        if ($responseTransfer->getIsSuccess() === false && $responseTransfer->getError() !== null) {
139
            return $responseTransfer->getError()->getMessage();
140
        }
141
142
        $method = sprintf(static::GET_RESPONSE_METHOD, ucfirst($requestType));
143
144
        if (!method_exists($responseTransfer, $method)) {
145
            throw new InvalidRequestTypeException();
146
        }
147
148
        return $responseTransfer->$method()->getMessage();
149
    }
150
151
    /**
152
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
153
     * @param string $requestType
154
     *
155
     * @throws \SprykerEco\Zed\CrefoPayApi\Business\Exception\InvalidRequestTypeException
156
     *
157
     * @return string|null
158
     */
159
    protected function getSalt(CrefoPayApiResponseTransfer $responseTransfer, string $requestType): ?string
160
    {
161
        if ($responseTransfer->getIsSuccess() === false && $responseTransfer->getError() !== null) {
162
            return $responseTransfer->getError()->getSalt();
163
        }
164
165
        $method = sprintf(static::GET_RESPONSE_METHOD, ucfirst($requestType));
166
167
        if (!method_exists($responseTransfer, $method)) {
168
            throw new InvalidRequestTypeException();
169
        }
170
171
        return $responseTransfer->$method()->getSalt();
172
    }
173
}
174