BaseOAuthProvider::doRequest()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 1 Features 2
Metric Value
dl 0
loc 21
ccs 0
cts 13
cp 0
rs 9.3143
c 3
b 1
f 2
cc 3
eloc 14
nc 3
nop 3
crap 12
1
<?php
2
3
namespace Sleepness\UberOAuthRestBundle\Provider;
4
5
use Sleepness\UberOAuthRestBundle\Exception\BadRequestException;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\ClientException;
8
9
abstract class BaseOAuthProvider implements OAuthProviderInterface
10
{
11
    const GET = 'GET';
12
    const POST = 'POST';
13
14
    /**
15
     * @var Client
16
     */
17
    protected $client;
18
19
    /**
20
     * @var array
21
     */
22
    protected $credentials;
23
24
    /**
25
     * BaseOAuthProvider constructor.
26
     *
27
     * @param $client
28
     */
29
    public function __construct($client)
30
    {
31
        $this->client = $client;
32
    }
33
34
    /**
35
     * @param array $credentials
36
     */
37
    public function setCredentials(array $credentials)
38
    {
39
        $this->credentials = $credentials;
40
    }
41
42
    /**
43
     * @param $url
44
     * @param $method
45
     * @param array $options
46
     *
47
     * @return mixed|\Psr\Http\Message\ResponseInterface
48
     */
49
    protected function doRequest($url, $method, array $options = [])
50
    {
51
        try {
52
            $response = $this->client->request(
53
                strtoupper($method),
54
                $url,
55
                $options
56
            );
57
        } catch (ClientException $e) {
58
            if ($e->hasResponse()) {
59
                throw new BadRequestException(
60
                    'Error while sending request',
61
                    $this->credentials['provider_name'],
62
                    $e->getCode(),
63
                    $e
64
                );
65
            }
66
        }
67
68
        return json_decode($response->getBody()->getContents());
69
    }
70
71
    /**
72
     * @param $url
73
     * @param array $parameters
74
     *
75
     * @return string
76
     */
77 3
    protected function normalizeUrl($url, array $parameters = array())
78
    {
79 3
        $normalizedUrl = $url;
80 3
        if (!empty($parameters)) {
81 2
            $normalizedUrl .= (false !== strpos($url, '?') ? '&' : '?').http_build_query($parameters, '', '&');
82 2
        }
83
84 3
        return $normalizedUrl;
85
    }
86
}
87