Passed
Pull Request — feature/eco-2295/dev (#1)
by Aleksey
05:36 queued 03:07
created

createPaymentCrefoPayApiLogTransfer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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