AbstractGateway::fetchCollection()   A
last analyzed

Complexity

Conditions 4
Paths 7

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.9102

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 18
ccs 8
cts 13
cp 0.6153
rs 9.8666
c 0
b 0
f 0
cc 4
nc 7
nop 3
crap 4.9102
1
<?php
2
/*
3
 * This file is part of the PayBreak/paybreak-sdk-php package.
4
 *
5
 * (c) PayBreak <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PayBreak\Sdk\Gateways;
12
13
use PayBreak\Sdk\ApiClient\ApiClientFactoryInterface;
14
use Psr\Log\LoggerInterface;
15
use WNowicki\Generic\Logger\PsrLoggerTrait;
16
use WNowicki\Generic\Exception;
17
use PayBreak\ApiClient\ErrorResponseException;
18
19
/**
20
 * Abstract Gateway
21
 *
22
 * @author WN
23
 * @package PayBreak\Sdk\Gateways
24
 */
25
abstract class AbstractGateway
26
{
27
    use PsrLoggerTrait;
28
29
    private $apiFactory;
30
    private $logger;
31
32
    /**
33
     * @author WN
34
     * @param ApiClientFactoryInterface $factory
35
     * @param LoggerInterface $logger
36
     */
37 56
    public function __construct(ApiClientFactoryInterface $factory, LoggerInterface $logger = null)
38
    {
39 56
        $this->apiFactory = $factory;
40 56
        $this->logger = $logger;
41 56
    }
42
43
    /**
44
     * @author WN
45
     * @return ApiClientFactoryInterface
46
     */
47 44
    protected function getApiFactory()
48
    {
49 44
        return $this->apiFactory;
50
    }
51
52
    /**
53
     * @author EB
54
     * @param $type
55
     * @param $documentPath
56
     * @param $token
57
     * @param $documentName
58
     * @param array $query
59
     * @param array $body
60
     * @return array
61
     * @throws Exception
62
     */
63 39
    private function makeRequestForDocument($type, $documentPath, $token, $documentName, array $query, array $body = [])
64
    {
65
        try {
66 39
            return $this->makeRequest($type, $documentPath, $token, $query, $body);
67 4
        } catch (ErrorResponseException $e) {
68 2
            throw new Exception($e->getMessage());
69 2
        } catch (\Exception $e) {
70 2
            $this->logError(
71 2
                $documentName . 'Gateway::' . $type .' '. $documentName . '[' . $e->getCode() . ']: ' . $e->getMessage()
72 2
            );
73 2
            throw new Exception('Problem with '.$type. ': ' . $documentName . ' data form Provider API');
74
        }
75
    }
76
77
    /**
78
     * @author EB
79
     * @param $type
80
     * @param $documentPath
81
     * @param $token
82
     * @param array $query
83
     * @param array $body
84
     * @return array
85
     */
86 39
    private function makeRequest($type, $documentPath, $token, array $query, array $body = [])
87
    {
88 39
        $api = $this->getApiFactory()->makeApiClient($token);
89
90
        switch ($type) {
91 39
            case 'post':
92 8
                return $api->post($documentPath, $body, $query);
93 31
            case 'delete':
94 1
                return $api->delete($documentPath, $query);
95 30
            case 'patch':
96
                return $api->patch($documentPath, $body, $query);
97 30
            default:
98 30
                return $api->get($documentPath, $query);
99 30
        }
100
    }
101
102
    /**
103
     * @author WN
104
     * @param $documentPath
105
     * @param $token
106
     * @param $documentName
107
     * @param array $query
108
     * @return array
109
     * @throws Exception
110
     */
111 30
    protected function fetchDocument($documentPath, $token, $documentName, array $query = [])
112
    {
113 30
        return $this->makeRequestForDocument('get', $documentPath, $token, $documentName, $query);
114
    }
115
116
    /**
117
     * @author EB
118
     * @param $documentPath
119
     * @param array $body
120
     * @param $token
121
     * @param $documentName
122
     * @return array
123
     * @throws Exception
124
     */
125 8
    protected function postDocument($documentPath, array $body = [], $token, $documentName)
126
    {
127 8
        return $this->makeRequestForDocument('post', $documentPath, $token, $documentName, [], $body);
128
    }
129
130
    /**
131
     * @author EB
132
     * @param $documentPath
133
     * @param $token
134
     * @param $documentName
135
     * @param array $body
136
     * @return array
137
     * @throws Exception
138
     */
139
    protected function patchDocument($documentPath, $token, $documentName, array $body = [])
140
    {
141
        return $this->makeRequestForDocument('patch', $documentPath, $token, $documentName, [], $body);
142
    }
143
144
    /**
145
     * @author LH
146
     * @param $token
147
     * @param $url
148
     * @param $entity
149
     * @return array
150
     * @throws Exception
151
     */
152 2
    public function fetchCollection($token, $url, $entity)
153
    {
154 2
        $api = $this->getApiFactory()->makeApiClient($token);
155
156
        try {
157 2
            $data = $api->get($url);
158 2
            $rtn = [];
159
160 2
            foreach ($data as $item) {
161 1
                $rtn[] = $entity::make($item);
162 2
            }
163
164 2
            return $rtn;
165
        } catch (ErrorResponseException $e) {
166
            throw new Exception($e->getMessage());
167
        } catch (\Exception $e) {
168
            $this->logError('Couldn\'t fetch collection of [' . $entity . ']: [' . $e->getCode() . ']: ' . $e->getMessage());
169
            throw new Exception('Problem fetching collection of [' . $entity . '] form Provider API');
170
        }
171
    }
172
173
    /**
174
     * @author EB
175
     * @param $documentPath
176
     * @param $token
177
     * @param $documentName
178
     * @return array
179
     * @throws Exception
180
     */
181 1
    protected function deleteDocument($documentPath, $token, $documentName)
182
    {
183 1
        return $this->makeRequestForDocument('delete', $documentPath, $token, $documentName, $query = []);
184
    }
185
186
    /**
187
     * @author WN
188
     * @return \Psr\Log\LoggerInterface|null
189
     */
190 4
    protected function getLogger()
191
    {
192 4
        return $this->logger;
193
    }
194
}
195