Completed
Push — master ( 35d0cd...0aa8d0 )
by Raphaël
02:11
created

CertainApiClient::getClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Wabel\CertainAPI;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use Wabel\CertainAPI\Response\CertainResponse;
8
use GuzzleHttp\Message\ResponseInterface;
9
10
/**
11
 * CertainApiClient
12
 * @see http://developer.certain.com/api2docs/introduction
13
 */
14
class CertainApiClient
15
{
16
    /**
17
     * URL for call request
18
     *
19
     * @var string
20
     */
21
    private $baseUri = 'https://appeu.certain.com/certainExternal/service/v1/';
22
23
    /**
24
     *
25
     * @var Client
26
     */
27
    private $client;
28
29
    /**
30
     * AccountCode to put in the URI
31
     *
32
     * @var string
33
     */
34
    private $accountCode;
35
36
    /**
37
     *
38
     * @param string|null $baseUri
39
     * @param string $username
40
     * @param string $password
41
     * @param string $accountCode
42
     */
43
    public function __construct($baseUri = null, $username, $password,
44
                                $accountCode)
45
    {
46
        if ($baseUri !== null) {
47
            $this->baseUri = $baseUri;
48
        }
49
        $this->accountCode = $accountCode;
50
        $this->setClient(new Client([
51
            'base_url' => $this->baseUri,
52
            'defaults' => [
53
                'auth' => [$username, $password],
54
            ]
55
            ]
56
        ));
57
    }
58
59
    /**
60
     *
61
     * @return Client
62
     */
63
    public function getClient()
64
    {
65
        return $this->client;
66
    }
67
68
    /**
69
     * Define a client
70
     * @param Client $client
71
     * @return \Wabel\CertainAPI\CertainApiClient
72
     */
73
    public function setClient(Client $client)
74
    {
75
        $this->client = $client;
76
        return $this;
77
    }
78
79
    /**
80
     * Get Account Code
81
     * @return string
82
     */
83
    public function getAccountCode()
84
    {
85
        return $this->accountCode;
86
    }
87
88
    /**
89
     * Build the URI to request
90
     * @param string|array $ressourceName
91
     * @param string $ressourceId
92
     * @return string
93
     */
94
    private function builPathToCall($ressourceName,$ressourcePath =null, $ressourceId = null)
95
    {
96
        $ressourceAdded = '';
97
        if(!is_null($ressourcePath) && $ressourcePath != ''){
98
            $ressourceAdded = $ressourcePath;
99
        }
100
        
101
        if ($ressourceId !== null) {
102
            $ressourceAdded = '/'.$ressourceId;
103
        }
104
        if(!is_null($ressourcePath)){
105
            return  'accounts/'.$this->getAccountCode().$ressourceAdded;
106
        }else{
107
            return $ressourceName.'/'.$this->getAccountCode().$ressourceAdded;
108
        }
109
        
110
    }
111
112
    /**
113
     * Make "GET" request with the client.
114
     * @param string $ressourceName
115
     * @param string $ressourcePath
116
     * @param string $ressourceId
117
     * @param array $query
118
     * @param boolean $assoc
119
     * @param string $contentType
120
     * @return array
121
     */
122 View Code Duplication
    public function get($ressourceName, $ressourcePath =null, $ressourceId = null, $query = array(),
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
                        $assoc = false, $contentType = 'json')
124
    {
125
        try {
126
            $urlRessource = $this->builPathToCall($ressourceName, $ressourcePath, $ressourceId);
127
            $response     = $this->getClient()->get($urlRessource,
128
                array(
129
                'headers' => ['Accept' => "application/$contentType"],
130
                'query' => $query
131
            ));
132
        } catch (ClientException $ex) {
133
            $response = $ex->getResponse();
134
        }
135
        return $this->makeCertainApiResponse($response, $contentType, $assoc);
136
    }
137
138
    /**
139
     * Make "POST" request with the client.
140
     * @param string $ressourceName
141
     * @param string $ressourcePath
142
     * @param string $ressourceId
143
     * @param array $bodyData
144
     * @param array $query
145
     * @param boolean $assoc
146
     * @param string $contentType
147
     * @return array
148
     */
149 View Code Duplication
    public function post($ressourceName, $ressourcePath =null, $ressourceId = null,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
                         $bodyData = array(), $query = array(), $assoc = false,
151
                         $contentType = 'json')
152
    {
153
        if ($contentType !== 'json') {
154
            throw new \Exception('Use only json to update or create');
155
        }
156
        try {
157
            $urlRessource = $this->builPathToCall($ressourceName, $ressourcePath, $ressourceId);
158
            $response     = $this->getClient()->post($urlRessource,
159
                array(
160
                'headers' => ['Accept' => "application/$contentType"],
161
                'json' => $bodyData,
162
                'query' => $query
163
            ));
164
        } catch (ClientException $ex) {
165
            $response = $ex->getResponse();
166
        }
167
        return $this->makeCertainApiResponse($response, $contentType, $assoc);
168
    }
169
170
    /**
171
     * Make "PUT" request with the client.
172
     * @param string $ressourceName
173
     * @param string $ressourcePath
174
     * @param string $ressourceId
175
     * @param array $bodyData
176
     * @param array $query
177
     * @param boolean $assoc
178
     * @param string $contentType
179
     * @return array
180
     */
181 View Code Duplication
    public function put($ressourceName, $ressourcePath =null, $ressourceId = null,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
                         $bodyData = array(), $query = array(), $assoc = false,
183
                         $contentType = 'json')
184
    {
185
        if ($contentType !== 'json') {
186
            throw new \Exception('Use only json to update or create');
187
        }
188
        try {
189
            $urlRessource = $this->builPathToCall($ressourceName, $ressourcePath, $ressourceId);
190
            $response     = $this->getClient()->put($urlRessource,
191
                array(
192
                'headers' => ['Accept' => "application/$contentType"],
193
                'json' => $bodyData,
194
                'query' => $query
195
            ));
196
        } catch (ClientException $ex) {
197
            $response = $ex->getResponse();
198
        }
199
        return $this->makeCertainApiResponse($response, $contentType, $assoc);
200
    }
201
    
202
    /**
203
     * Make "DELETE" request with the client.
204
     * @param string $ressourceName
205
     * @param string $ressourcePath
206
     * @param string $ressourceId
207
     * @param boolean $assoc
208
     * @param string $contentType
209
     * @return array
210
     */
211 View Code Duplication
    public function delete($ressourceName, $ressourcePath =null, $ressourceId = null, $assoc = false,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
212
                           $contentType = 'json')
213
    {
214
        try {
215
            $urlRessource = $this->builPathToCall($ressourceName, $ressourcePath, $ressourceId);
216
            $response     = $this->getClient()->delete($urlRessource,
217
                array(
218
                'headers' => ['Accept' => "application/$contentType"],
219
            ));
220
        } catch (ClientException $ex) {
221
            $response = $ex->getResponse();
222
        }
223
        return $this->makeCertainApiResponse($response, $contentType, $assoc);
224
    }
225
226
    /**
227
     * Make the  Certain Api repsonse.
228
     * @param ResponseInterface $response
229
     * @param string $contentType
230
     * @param boolean $assoc
231
     * @return array
232
     */
233
    private function makeCertainApiResponse(ResponseInterface $response,
234
                                            $contentType = 'json', $assoc = false)
235
    {
236
237
        $responseCertainApi = new CertainResponse($response);
238
        return $responseCertainApi->getResponse($contentType, $assoc);
239
    }
240
}