BaseOAuthProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 24%

Importance

Changes 4
Bugs 2 Features 2
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 78
ccs 6
cts 25
cp 0.24
rs 10
c 4
b 2
f 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setCredentials() 0 4 1
A doRequest() 0 21 3
A normalizeUrl() 0 9 3
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