Passed
Push — master ( 75afe1...a75cb7 )
by smiley
01:30
created

NPROne::sendRequest()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 15
rs 9.6111
cc 5
nc 3
nop 1
1
<?php
2
/**
3
 * Class NPROne
4
 *
5
 * @link https://dev.npr.org
6
 * @link https://github.com/npr/npr-one-backend-proxy-php
7
 *
8
 * @filesource   NPROne.php
9
 * @created      28.07.2019
10
 * @package      chillerlan\OAuth\Providers\NPR
11
 * @author       smiley <[email protected]>
12
 * @copyright    2019 smiley
13
 * @license      MIT
14
 */
15
16
namespace chillerlan\OAuth\Providers\NPR;
17
18
use chillerlan\OAuth\Core\{CSRFToken, OAuth2Provider, TokenRefresh};
19
use Psr\Http\Message\{RequestInterface, ResponseInterface};
20
21
/**
22
 * @method \Psr\Http\Message\ResponseInterface identityFollowing(array $body = ['Affiliation'])
23
 * @method \Psr\Http\Message\ResponseInterface identityInherit(array $body = ['UserDocument'])
24
 * @method \Psr\Http\Message\ResponseInterface identityStations(array $body = ['StationIDs'])
25
 * @method \Psr\Http\Message\ResponseInterface identityUser()
26
 * @method \Psr\Http\Message\ResponseInterface listeningAggregationRecommendations(string $aggId)
27
 * @method \Psr\Http\Message\ResponseInterface listeningChannels()
28
 * @method \Psr\Http\Message\ResponseInterface listeningHistory()
29
 * @method \Psr\Http\Message\ResponseInterface listeningOrgCategoryRecommendations(string $orgId, string $category)
30
 * @method \Psr\Http\Message\ResponseInterface listeningOrgRecommendations(string $orgId)
31
 * @method \Psr\Http\Message\ResponseInterface listeningPromoRecommendations()
32
 * @method \Psr\Http\Message\ResponseInterface listeningRatings(array $body = ['RatingData'])
33
 * @method \Psr\Http\Message\ResponseInterface listeningRecommendations()
34
 * @method \Psr\Http\Message\ResponseInterface listeningSearchRecommendations()
35
 * @method \Psr\Http\Message\ResponseInterface station(string $stationId)
36
 * @method \Psr\Http\Message\ResponseInterface stations()
37
 */
38
class NPROne extends OAuth2Provider implements CSRFToken, TokenRefresh{
39
40
	public const IDENTITY_READONLY  = 'identity.readonly';
41
	public const IDENTITY_WRITE     = 'identity.write';
42
	public const LISTENING_READONLY = 'listening.readonly';
43
	public const LISTENING_WRITE    = 'listening.write';
44
	public const LOCALACTIVATION    = 'localactivation';
45
46
	protected $authURL        = 'https://authorization.api.npr.org/v2/authorize';
47
	protected $accessTokenURL = 'https://authorization.api.npr.org/v2/token';
48
	protected $revokeURL      = 'https://authorization.api.npr.org/v2/token/revoke';
49
	protected $endpointMap    = NPROneEndpoints::class;
50
	protected $apiDocs        = 'https://dev.npr.org/api/';
51
	protected $applicationURL = 'https://dev.npr.org/console';
52
53
	/**
54
	 * @param \Psr\Http\Message\RequestInterface $request
55
	 *
56
	 * @return \Psr\Http\Message\ResponseInterface
57
	 */
58
	public function sendRequest(RequestInterface $request):ResponseInterface{
59
60
		// get authorization only if we request the provider API
61
		if(\strpos((string)$request->getUri(), '.api.npr.org/') !== false){ // silly resource subdomains >.<
62
			$token = $this->storage->getAccessToken($this->serviceName);
63
64
			// attempt to refresh an expired token
65
			if($this->options->tokenAutoRefresh && ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)){
66
				$token = $this->refreshAccessToken($token); // @codeCoverageIgnore
67
			}
68
69
			$request = $this->getRequestAuthorization($request, $token);
70
		}
71
72
		return $this->http->sendRequest($request);
73
	}
74
75
}
76