Harvest   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 95.45%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 10
c 3
b 1
f 0
lcom 1
cbo 5
dl 0
loc 114
ccs 21
cts 22
cp 0.9545
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createResourceOwner() 0 6 1
A getBaseAuthorizationUrl() 0 4 1
A getBaseAccessTokenUrl() 0 4 1
A getResourceOwnerDetailsUrl() 0 7 2
A getDefaultScopes() 0 4 1
A checkResponse() 0 8 3
A getAuthenticatedRequest() 0 5 1
1
<?php
2
3
namespace Nilesuan\OAuth2\Client\Provider;
4
5
use League\OAuth2\Client\Provider\AbstractProvider;
6
use Nilesuan\OAuth2\Client\Provider\Exception\HarvestIdentityProviderException;
7
use League\OAuth2\Client\Token\AccessToken;
8
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
9
use Psr\Http\Message\ResponseInterface;
10
11
class Harvest extends AbstractProvider
12
{
13
    use BearerAuthorizationTrait;
14
15
    /**
16
     * Domain
17
     *
18
     * @var string
19
     */
20
    public $domain = 'https://api.harvestapp.com';
21
22
    /**
23
     * Api domain
24
     *
25
     * @var string
26
     */
27
    public $apiDomain = 'https://api.harvestapp.com';
28
29
    /**
30
     * Get authorization url to begin OAuth flow
31
     *
32
     * @return string
33
     */
34 3
    public function getBaseAuthorizationUrl()
35
    {
36 3
        return $this->domain.'/oauth2/authorize';
37
    }
38
39
    /**
40
     * Get access token url to retrieve token
41
     *
42
     * @param  array $params
43
     *
44
     * @return string
45
     */
46 5
    public function getBaseAccessTokenUrl(array $params)
47
    {
48 5
        return $this->domain.'/oauth2/token';
49
    }
50
51
    /**
52
     * Get provider url to fetch user details
53
     *
54
     * @param  AccessToken $token
55
     *
56
     * @return string
57
     */
58 1
    public function getResourceOwnerDetailsUrl(AccessToken $token)
59
    {
60 1
        if ($this->domain === 'https://api.harvestapp.com') {
61 1
            return $this->apiDomain.'/account/who_am_i';
62
        }
63
        return $this->domain.'/account/who_am_i';
64
    }
65
66
    /**
67
     * Get the default scopes used by this provider.
68
     *
69
     * This should not be a complete list of all scopes, but the minimum
70
     * required for the provider user interface!
71
     *
72
     * @return array
73
     */
74 2
    protected function getDefaultScopes()
75
    {
76 2
        return [];
77
    }
78
79
    /**
80
     * Check a provider response for errors.
81
     *
82
     * @throws HarvestIdentityProviderException
83
     * @param  ResponseInterface $response
84
     * @param  string $data Parsed response data
85
     * @return void
86
     */
87 4
    protected function checkResponse(ResponseInterface $response, $data)
88
    {
89 4
        if ($response->getStatusCode() >= 400) {
90 1
            throw HarvestIdentityProviderException::clientException($response, $data);
91 3
        } elseif (isset($data['error'])) {
92 1
            throw HarvestIdentityProviderException::oauthException($response, $data);
93
        }
94 2
    }
95
96
    /**
97
     * Generate a user object from a successful user details request.
98
     *
99
     * @param array $response
100
     * @param AccessToken $token
101
     * @return League\OAuth2\Client\Provider\ResourceOwnerInterface
102
     */
103 1
    protected function createResourceOwner(array $response, AccessToken $token)
104
    {
105 1
        $user = new HarvestResourceOwner($response);
106
107 1
        return $user->setDomain($this->domain);
108
    }
109
110
    /**
111
     * Returns an authenticated PSR-7 request instance.
112
     *
113
     * @param  string $method
114
     * @param  string $url
115
     * @param  AccessToken|string $token
116
     * @param  array $options Any of "headers", "body", and "protocolVersion".
117
     * @return RequestInterface
118
     */
119 1
    public function getAuthenticatedRequest($method, $url, $token, array $options = [])
120
    {
121 1
        $options['headers'] = array('Content-Type' => 'application/json','Accept' => 'application/json');
122 1
        return $this->createRequest($method, $url, $token, $options);
123
    }
124
}
125