Passed
Push — master ( 778b27...ab5cea )
by Dominic
02:27
created

AuthorizationCodeRequest::generateState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace tbclla\Revolut\Auth\Requests;
4
5
class AuthorizationCodeRequest
6
{
7
	/**
8
	 * The sandbox URL
9
	 * 
10
	 * @var string
11
	 */
12
	const SANDBOX_URL = 'https://sandbox-business.revolut.com';
13
14
	/**
15
	 * The production URL
16
	 * 
17
	 * @var string
18
	 */
19
	const PRODUCTION_URL = 'https://business.revolut.com';
20
21
	/**
22
	 * The app authorization endpoint
23
	 * 
24
	 * @var string
25
	 */
26
	const ENDPOINT = '/app-confirm';
27
28
	/**
29
	 * A token repository
30
	 *
31
	 * @var string The client Id
32
	 */
33
	private $clientId;
34
35
	/**
36
	 * A token repository
37
	 *
38
	 * @var string The redirect URI
39
	 */
40
	private $redirectUri;
41
42
	/**
43
	 * A token repository
44
	 *
45
	 * @var bool The environment
46
	 */
47
	private $sandbox;
48
49
	/**
50
	 * A state value
51
	 *
52
	 * @var string
53
	 */
54
	public $state;
55
56
	/**
57
	 * Create a new request
58
	 *
59
	 * @param string $clientId The Revolut Business Client ID
60
	 * @param string $redirectUri The OAuth redirect URI
61
	 * @param bool $sandbox Whether or not to use the sandbox environment
62
	 * @return void
63
	 */
64
	public function __construct(string $clientId, string $redirectUri, bool $sandbox = true)
65
	{
66
		$this->clientId = $clientId;
67
		$this->redirectUri = $redirectUri;
68
		$this->sandbox = $sandbox;
69
		$this->state = $this->generateState();
70
	}
71
72
	/**
73
	 * Build the request
74
	 * 
75
	 * @return string
76
	 */
77
	public function build()
78
	{
79
		return $this->baseUri() . self::ENDPOINT . '?' . $this->buildQuery();
80
	}
81
82
	/**
83
	 * Build the base URI
84
	 * 
85
	 * @return string
86
	 */
87
	private function baseUri()
88
	{
89
		return $this->sandbox ? self::SANDBOX_URL : self::PRODUCTION_URL;
90
	}
91
92
	/**
93
	 * Build the query
94
	 * 
95
	 * @return string
96
	 */
97
	private function buildQuery()
98
	{
99
		return http_build_query([
100
			'response_type' => 'request_token',
101
			'client_id' => $this->clientId,
102
			'redirect_uri' => $this->redirectUri,
103
			'state' => $this->state
104
		]);
105
	}
106
107
	/**
108
	 * Generate a state value
109
	 *
110
	 * @return string
111
	 */
112
	private function generateState()
113
	{
114
		return base64_encode(random_bytes(32));
115
	}
116
}
117