Foursquare   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 21
c 1
b 0
f 0
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A me() 0 15 3
A request() 0 12 1
1
<?php
2
/**
3
 * Class Foursquare
4
 *
5
 * @created      10.08.2018
6
 * @author       Smiley <[email protected]>
7
 * @copyright    2018 Smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\OAuth\Providers;
12
13
use chillerlan\HTTP\Utils\MessageUtil;
14
use chillerlan\HTTP\Utils\QueryUtil;
15
use chillerlan\OAuth\Core\OAuth2Provider;
16
use chillerlan\OAuth\Core\ProviderException;
17
use Psr\Http\Message\{ResponseInterface, StreamInterface};
18
use function array_merge;
19
use function explode;
20
use function sprintf;
21
22
/**
23
 * @see https://developer.foursquare.com/docs/
24
 * @see https://developer.foursquare.com/overview/auth
25
 */
26
class Foursquare extends OAuth2Provider{
27
28
	protected const API_VERSIONDATE = '20190225';
29
30
	protected string  $authURL         = 'https://foursquare.com/oauth2/authenticate';
31
	protected string  $accessTokenURL  = 'https://foursquare.com/oauth2/access_token';
32
	protected string  $apiURL          = 'https://api.foursquare.com';
33
	protected ?string $userRevokeURL   = 'https://foursquare.com/settings/connections';
34
	protected ?string $apiDocs         = 'https://developer.foursquare.com/docs';
35
	protected ?string $applicationURL  = 'https://foursquare.com/developers/apps';
36
	protected string  $authMethodQuery = 'oauth_token';
37
	protected int     $authMethod      = self::AUTH_METHOD_QUERY;
38
39
	/**
40
	 * @inheritDoc
41
	 */
42
	public function request(
43
		string $path,
44
		array $params = null,
45
		string $method = null,
46
		StreamInterface|array|string $body = null,
47
		array $headers = null
48
	):ResponseInterface{
49
		$queryparams      = QueryUtil::parse(QueryUtil::parseUrl($this->apiURL.$path)['query'] ?? '');
50
		$queryparams['v'] = $this::API_VERSIONDATE;
51
		$queryparams['m'] = 'foursquare';
52
53
		return parent::request(explode('?', $path)[0], array_merge($params ?? [], $queryparams), $method, $body, $headers);
54
	}
55
56
	/**
57
	 * @inheritDoc
58
	 */
59
	public function me():ResponseInterface{
60
		$response = $this->request('/v2/users/self');
61
		$status   = $response->getStatusCode();
62
63
		if($status === 200){
64
			return $response;
65
		}
66
67
		$json = MessageUtil::decodeJSON($response);
68
69
		if(isset($json->meta, $json->meta->errorDetail)){
70
			throw new ProviderException($json->meta->errorDetail);
71
		}
72
73
		throw new ProviderException(sprintf('user info error error HTTP/%s', $status));
74
	}
75
76
}
77