Completed
Push — master ( bcce86...53605b )
by Raphaël
02:16
created

CertainApiClient::getBuilPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 Psr\Http\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
    private $builPath;
37
38
    /**
39
     *
40
     * @param string|null $baseUri
41
     * @param string $username
42
     * @param string $password
43
     * @param string $accountCode
44
     */
45
    public function __construct($baseUri = null, $username, $password,
46
                                $accountCode)
47
    {
48
        if ($baseUri !== null) {
49
            $this->baseUri = $baseUri;
50
        }
51
        $this->accountCode = $accountCode;
52
        $this->setClient(new Client([
53
            'base_uri' => $this->baseUri,
54
            'defaults' => [
55
                'auth' => [$username, $password],
56
            ]
57
            ]
58
        ));
59
    }
60
61
    /**
62
     *
63
     * @return Client
64
     */
65
    public function getClient()
66
    {
67
        return $this->client;
68
    }
69
70
    /**
71
     * Define a client
72
     * @param Client $client
73
     * @return \Wabel\CertainAPI\CertainApiClient
74
     */
75
    public function setClient(Client $client)
76
    {
77
        $this->client = $client;
78
        return $this;
79
    }
80
81
    /**
82
     * Get Account Code
83
     * @return string
84
     */
85
    public function getAccountCode()
86
    {
87
        return $this->accountCode;
88
    }
89
90
    /**
91
     * Build the URI to request
92
     * @param string|array $ressourceName
93
     * @param string $ressourceId
94
     * @return string
95
     */
96
    private function builPathToCall($ressourceName,$ressourcePath =null, $ressourceId = null)
97
    {
98
        $ressourceAdded = '';
99
        if(!is_null($ressourcePath) && $ressourcePath != ''){
100
            $ressourceAdded = $ressourcePath;
101
        }
102
        
103
        if ($ressourceId !== null) {
104
            $ressourceAdded = '/'.$ressourceId;
105
        }
106
        if(!is_null($ressourcePath)){
107
            $this->builPath = 'accounts/'.$this->getAccountCode().$ressourceAdded;
108
            return  $this->builPath;
109
        }else{
110
            $this->builPath = $ressourceName.'/'.$this->getAccountCode().$ressourceAdded;
111
            return  $this->builPath;
112
        }
113
        
114
    }
115
116
    /**
117
     * Make "GET" request with the client.
118
     * @param string $ressourceName
119
     * @param string $ressourcePath
120
     * @param string $ressourceId
121
     * @param array $query
122
     * @param boolean $assoc
123
     * @param string $contentType
124
     * @return array
125
     */
126 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...
127
                        $assoc = false, $contentType = 'json')
128
    {
129
        try {
130
            $urlRessource = $this->builPathToCall($ressourceName, $ressourcePath, $ressourceId);
131
            $response     = $this->getClient()->get($urlRessource,
132
                array(
133
                'headers' => ['Accept' => "application/$contentType"],
134
                'query' => $query
135
            ));
136
        } catch (ClientException $ex) {
137
            $response = $ex->getResponse();
138
        }
139
        return $this->makeCertainApiResponse($response, $contentType, $assoc);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $ex->getResponse() on line 137 can be null; however, Wabel\CertainAPI\Certain...akeCertainApiResponse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
140
    }
141
142
    /**
143
     * Make "POST" request with the client.
144
     * @param string $ressourceName
145
     * @param string $ressourcePath
146
     * @param string $ressourceId
147
     * @param array $bodyData
148
     * @param array $query
149
     * @param boolean $assoc
150
     * @param string $contentType
151
     * @return array
152
     */
153 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...
154
                         $bodyData = array(), $query = array(), $assoc = false,
155
                         $contentType = 'json')
156
    {
157
        if ($contentType !== 'json') {
158
            throw new \Exception('Use only json to update or create');
159
        }
160
        try {
161
            $urlRessource = $this->builPathToCall($ressourceName, $ressourcePath, $ressourceId);
162
            $response     = $this->getClient()->post($urlRessource,
163
                array(
164
                'headers' => ['Accept' => "application/$contentType"],
165
                'json' => $bodyData,
166
                'query' => $query
167
            ));
168
        } catch (ClientException $ex) {
169
            $response = $ex->getResponse();
170
        }
171
        return $this->makeCertainApiResponse($response, $contentType, $assoc);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $ex->getResponse() on line 169 can be null; however, Wabel\CertainAPI\Certain...akeCertainApiResponse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
172
    }
173
174
    /**
175
     * Make "PUT" request with the client.
176
     * @param string $ressourceName
177
     * @param string $ressourcePath
178
     * @param string $ressourceId
179
     * @param array $bodyData
180
     * @param array $query
181
     * @param boolean $assoc
182
     * @param string $contentType
183
     * @return array
184
     */
185 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...
186
                         $bodyData = array(), $query = array(), $assoc = false,
187
                         $contentType = 'json')
188
    {
189
        if ($contentType !== 'json') {
190
            throw new \Exception('Use only json to update or create');
191
        }
192
        try {
193
            $urlRessource = $this->builPathToCall($ressourceName, $ressourcePath, $ressourceId);
194
            $response     = $this->getClient()->put($urlRessource,
195
                array(
196
                'headers' => ['Accept' => "application/$contentType"],
197
                'json' => $bodyData,
198
                'query' => $query
199
            ));
200
        } catch (ClientException $ex) {
201
            $response = $ex->getResponse();
202
        }
203
        return $this->makeCertainApiResponse($response, $contentType, $assoc);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $ex->getResponse() on line 201 can be null; however, Wabel\CertainAPI\Certain...akeCertainApiResponse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
204
    }
205
    
206
    /**
207
     * Make "DELETE" request with the client.
208
     * @param string $ressourceName
209
     * @param string $ressourcePath
210
     * @param string $ressourceId
211
     * @param boolean $assoc
212
     * @param string $contentType
213
     * @return array
214
     */
215 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...
216
                           $contentType = 'json')
217
    {
218
        try {
219
            $urlRessource = $this->builPathToCall($ressourceName, $ressourcePath, $ressourceId);
220
            $response     = $this->getClient()->delete($urlRessource,
221
                array(
222
                'headers' => ['Accept' => "application/$contentType"],
223
            ));
224
        } catch (ClientException $ex) {
225
            $response = $ex->getResponse();
226
        }
227
        return $this->makeCertainApiResponse($response, $contentType, $assoc);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $ex->getResponse() on line 225 can be null; however, Wabel\CertainAPI\Certain...akeCertainApiResponse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
228
    }
229
230
    /**
231
     * Make the  Certain Api repsonse.
232
     * @param ResponseInterface $response
233
     * @param string $contentType
234
     * @param boolean $assoc
235
     * @return array
236
     */
237
    private function makeCertainApiResponse(ResponseInterface $response,
238
                                            $contentType = 'json', $assoc = false)
239
    {
240
241
        $responseCertainApi = new CertainResponse($response);
242
        return $responseCertainApi->getResponse($contentType, $assoc);
243
    }
244
245
    public function getBuilPath()
246
    {
247
        return $this->builPath;
248
    }
249
250
251
}