Passed
Pull Request — feature/eco-2295/dev (#1)
by Aleksey
02:10 queued 01:12
created

CrefoPayApiLogger::logApiCall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 13
rs 10
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;
0 ignored issues
show
Bug introduced by
The type Spryker\Zed\Kernel\Persi...anager\TransactionTrait 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...
14
use SprykerEco\Zed\CrefoPayApi\Persistence\CrefoPayApiEntityManagerInterface;
15
16
class CrefoPayApiLogger implements CrefoPayApiLoggerInterface
17
{
18
    use TransactionTrait;
19
20
    protected const GET_REQUEST_METHOD = 'get%sRequest';
21
    protected const GET_RESPONSE_METHOD = 'get%sResponse';
22
23
    /**
24
     * @var \SprykerEco\Zed\CrefoPayApi\Persistence\CrefoPayApiEntityManagerInterface
25
     */
26
    protected $entityManager;
27
28
    /**
29
     * @var string
30
     */
31
    protected $requestType;
32
33
    /**
34
     * @param \SprykerEco\Zed\CrefoPayApi\Persistence\CrefoPayApiEntityManagerInterface $entityManager
35
     */
36
    public function __construct(CrefoPayApiEntityManagerInterface $entityManager)
37
    {
38
        $this->entityManager = $entityManager;
39
    }
40
41
    /**
42
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
43
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
44
     * @param string $requestType
45
     *
46
     * @return \Generated\Shared\Transfer\PaymentCrefoPayApiLogTransfer
47
     */
48
    public function logApiCall(
49
        CrefoPayApiRequestTransfer $requestTransfer,
50
        CrefoPayApiResponseTransfer $responseTransfer,
51
        string $requestType
52
    ): PaymentCrefoPayApiLogTransfer {
53
        $this->requestType = $requestType;
54
        $paymentCrefoPayApiLog = $this->createPaymentCrefoPayApiLogTransfer(
55
            $requestTransfer,
56
            $responseTransfer
57
        );
58
59
        return $this->getTransactionHandler()->handleTransaction(function () use ($paymentCrefoPayApiLog) {
60
            return $this->entityManager->savePaymentCrefoPayApiLog($paymentCrefoPayApiLog);
61
        });
62
    }
63
64
    /**
65
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
66
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
67
     *
68
     * @return \Generated\Shared\Transfer\PaymentCrefoPayApiLogTransfer
69
     */
70
    protected function createPaymentCrefoPayApiLogTransfer(
71
        CrefoPayApiRequestTransfer $requestTransfer,
72
        CrefoPayApiResponseTransfer $responseTransfer
73
    ): PaymentCrefoPayApiLogTransfer {
74
        return (new PaymentCrefoPayApiLogTransfer())
75
            ->setRequestType($this->requestType)
76
            ->setCrefoPayOrderId($this->getCrefoPayOrderId($requestTransfer))
77
            ->setIsSuccess($responseTransfer->getIsSuccess())
78
            ->setResultCode($this->getResultCode($responseTransfer))
79
            ->setMessage($this->getMessage($responseTransfer))
80
            ->setSalt($this->getSalt($responseTransfer))
81
            ->setRequest($requestTransfer->serialize())
82
            ->setResponse($responseTransfer->serialize());
83
    }
84
85
    /**
86
     * @param \Generated\Shared\Transfer\CrefoPayApiRequestTransfer $requestTransfer
87
     *
88
     * @return string
89
     */
90
    protected function getCrefoPayOrderId(CrefoPayApiRequestTransfer $requestTransfer): string
91
    {
92
        $method = sprintf(static::GET_REQUEST_METHOD, ucfirst($this->requestType));
93
94
        return $requestTransfer->$method()->getOrderID();
95
    }
96
97
    /**
98
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
99
     *
100
     * @return int|null
101
     */
102
    protected function getResultCode(CrefoPayApiResponseTransfer $responseTransfer): ?int
103
    {
104
        if ($responseTransfer->getIsSuccess() === false) {
105
            return $responseTransfer->getError()->getResultCode();
106
        }
107
108
        $method = sprintf(static::GET_RESPONSE_METHOD, ucfirst($this->requestType));
109
110
        return $responseTransfer->$method()->getResultCode();
111
    }
112
113
    /**
114
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
115
     *
116
     * @return string|null
117
     */
118
    protected function getMessage(CrefoPayApiResponseTransfer $responseTransfer): ?string
119
    {
120
        if ($responseTransfer->getIsSuccess() === false) {
121
            return $responseTransfer->getError()->getMessage();
122
        }
123
124
        $method = sprintf(static::GET_RESPONSE_METHOD, ucfirst($this->requestType));
125
126
        return $responseTransfer->$method()->getMessage();
127
    }
128
129
    /**
130
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
131
     *
132
     * @return string|null
133
     */
134
    protected function getSalt(CrefoPayApiResponseTransfer $responseTransfer): ?string
135
    {
136
        if ($responseTransfer->getIsSuccess() === false) {
137
            return $responseTransfer->getError()->getSalt();
138
        }
139
140
        $method = sprintf(static::GET_RESPONSE_METHOD, ucfirst($this->requestType));
141
142
        return $responseTransfer->$method()->getSalt();
143
    }
144
}
145