Completed
Pull Request — master (#466)
by
unknown
03:34
created

Wrike   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 113
rs 10
c 2
b 0
f 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 2
A getAuthorizationEndpoint() 0 4 1
A getAccessTokenEndpoint() 0 4 1
B parseAccessTokenResponse() 0 26 5
A getApiVersionString() 0 4 2
A getScopesDelimiter() 0 4 1
A getAuthorizationMethod() 0 4 1
1
<?php
2
/**
3
 * Wrike service.
4
 *
5
 * @author  Ádám Bálint <[email protected]>
6
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
7
 * @link    https://developers.wrike.com/documentation
8
 */
9
10
namespace OAuth\OAuth2\Service;
11
12
use OAuth\OAuth2\Token\StdOAuth2Token;
13
use OAuth\Common\Http\Exception\TokenResponseException;
14
use OAuth\Common\Http\Uri\Uri;
15
use OAuth\Common\Consumer\CredentialsInterface;
16
use OAuth\Common\Http\Client\ClientInterface;
17
use OAuth\Common\Storage\TokenStorageInterface;
18
use OAuth\Common\Http\Uri\UriInterface;
19
20
/**
21
 * Wrike service.
22
 *
23
 * @author  Ádám Bálint <[email protected]>
24
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
25
 * @link    https://developers.wrike.com/documentation
26
 */
27
class Wrike extends AbstractService
28
{
29
    /**
30
     * Defined scopes
31
     * https://developers.wrike.com/documentation/oauth2
32
     */
33
	const SCOPE_DEFAULT                 = 'Default';
34
	const SCOPE_READ                    = 'wsReadOnly';
35
	const SCOPE_READ_WRITE              = 'wsReadWrite';
36
	const SCOPE_WORKFLOW_READ           = 'amReadOnlyWorkflow';
37
	const SCOPE_WORKFLOW_READ_WRITE     = 'amReadWriteWorkflow';
38
	const SCOPE_INVITATION_READ         = 'amReadOnlyInvitation';
39
	const SCOPE_INVITATION_READ_WRITE   = 'amReadWriteInvitation';
40
	const SCOPE_GROUP_READ              = 'amReadOnlyGroup';
41
	const SCOPE_GROUP_READ_WRITE        = 'amReadWriteGroup';
42
	const SCOPE_USER_READ               = 'amReadOnlyUser';
43
	const SCOPE_USER_READ_WRITE         = 'amReadWriteUser';
44
45
46
	public function __construct(
47
		CredentialsInterface $credentials,
48
		ClientInterface $httpClient,
49
		TokenStorageInterface $storage,
50
		$scopes = array(),
51
		UriInterface $baseApiUri = null,
52
		$stateParameterInAutUrl = false,
53
		$apiVersion = "3" // The latest API version
54
	) {
55
		parent::__construct(
56
			$credentials,
57
			$httpClient,
58
			$storage,
59
			$scopes,
60
			$baseApiUri,
61
			$stateParameterInAutUrl,
62
			$apiVersion
63
		);
64
65
		if (null === $baseApiUri) {
66
			$this->baseApiUri = new Uri('https://www.wrike.com/api'.$this->getApiVersionString().'/');
67
		}
68
	}
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getAuthorizationEndpoint()
74
    {
75
		return new Uri('https://www.wrike.com/oauth2/authorize');
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getAccessTokenEndpoint()
82
    {
83
	    return new Uri('https://www.wrike.com/oauth2/token');
84
    }
85
86
	/**
87
	 * {@inheritdoc}
88
	 */
89
	protected function parseAccessTokenResponse($responseBody)
90
	{
91
		$data = json_decode($responseBody, true);
92
93
		if (null === $data || !is_array($data)) {
94
			throw new TokenResponseException('Unable to parse response.');
95
		} elseif (isset($data['error'])) {
96
			throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
97
		}
98
99
		$token = new StdOAuth2Token();
100
		$token->setAccessToken($data['access_token']);
101
		$token->setLifetime($data['expires_in']);
102
103
		if(isset($data['refresh_token'])){
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
104
			$token->setRefreshToken($data['refresh_token']);
105
			unset($data['refresh_token']);
106
		}
107
108
		unset($data['access_token']);
109
		unset($data['expires_in']);
110
111
		$token->setExtraParams($data);
112
113
		return $token;
114
	}
115
116
	/**
117
	 * {@inheritdoc}
118
	 */
119
	protected function getApiVersionString()
120
	{
121
		return empty($this->apiVersion) ? '' : '/v' . $this->apiVersion;
122
	}
123
124
	/**
125
	 * {@inheritdoc}
126
	 */
127
	protected function getScopesDelimiter()
128
	{
129
		return ',';
130
	}
131
132
	/**
133
	 * {@inheritdoc}
134
	 */
135
	protected function getAuthorizationMethod()
136
	{
137
		return static::AUTHORIZATION_METHOD_HEADER_BEARER;
138
	}
139
}
140