ApiVersionCall   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 12 2
A parseVersion() 0 9 3
A __construct() 0 8 1
A logApiException() 0 5 1
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\AfterPay\Business\Api\Adapter\ApiCall;
9
10
use Spryker\Shared\Log\LoggerTrait;
11
use SprykerEco\Shared\AfterPay\AfterPayApiRequestConfig;
12
use SprykerEco\Zed\AfterPay\AfterPayConfig;
13
use SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface;
14
use SprykerEco\Zed\AfterPay\Business\Exception\ApiHttpRequestException;
15
use SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface;
16
17
class ApiVersionCall implements ApiVersionCallInterface
18
{
19
    use LoggerTrait;
20
21
    /**
22
     * @var \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface
23
     */
24
    protected $client;
25
26
    /**
27
     * @var \SprykerEco\Zed\AfterPay\AfterPayConfig
28
     */
29
    protected $config;
30
31
    /**
32
     * @var \SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface
33
     */
34
    protected $utilEncoding;
35
36
    /**
37
     * @param \SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface $client
38
     * @param \SprykerEco\Zed\AfterPay\AfterPayConfig $config
39
     * @param \SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface $utilEncoding
40
     */
41
    public function __construct(
42
        ClientInterface $client,
43
        AfterPayConfig $config,
44
        AfterPayToUtilEncodingServiceInterface $utilEncoding
45
    ) {
46
        $this->client = $client;
47
        $this->config = $config;
48
        $this->utilEncoding = $utilEncoding;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function execute(): string
55
    {
56
        try {
57
            $jsonResponse = $this->client->sendGet(
58
                $this->config->getVersionApiEndpointUrl(),
59
            );
60
        } catch (ApiHttpRequestException $apiHttpRequestException) {
61
            $this->logApiException($apiHttpRequestException);
62
            $jsonResponse = '[]';
63
        }
64
65
        return $this->parseVersion($jsonResponse);
66
    }
67
68
    /**
69
     * @param string $jsonResponse
70
     *
71
     * @return string
72
     */
73
    protected function parseVersion(string $jsonResponse): string
74
    {
75
        $jsonResponseArray = $this->utilEncoding->decodeJson($jsonResponse, true);
76
77
        if (is_array($jsonResponseArray) && isset($jsonResponseArray[AfterPayApiRequestConfig::API_VERSION])) {
78
            return $jsonResponseArray[AfterPayApiRequestConfig::API_VERSION];
79
        }
80
81
        return '';
82
    }
83
84
    /**
85
     * @param \SprykerEco\Zed\AfterPay\Business\Exception\ApiHttpRequestException $apiHttpRequestException
86
     *
87
     * @return void
88
     */
89
    protected function logApiException(ApiHttpRequestException $apiHttpRequestException): void
90
    {
91
        $this->getLogger()->error(
92
            $apiHttpRequestException->getMessage(),
93
            ['exception' => $apiHttpRequestException],
94
        );
95
    }
96
}
97