1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Slackify. |
5
|
|
|
* |
6
|
|
|
* (c) Strime <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Strime\Slackify\Api; |
13
|
|
|
|
14
|
|
|
use Strime\Slackify\Exception\RuntimeException; |
15
|
|
|
use Strime\Slackify\Exception\InvalidArgumentException; |
16
|
|
|
use GuzzleHttp\Exception\RequestException; |
17
|
|
|
|
18
|
|
|
class Oauth extends AbstractApi |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @param string $client_id |
24
|
|
|
* @param string $client_secret |
25
|
|
|
* @param string $code |
26
|
|
|
* @param string $redirect_uri |
27
|
|
|
* @return string $response |
28
|
|
|
*/ |
29
|
|
|
public function access($client_id, $client_secret, $code, $redirect_uri = NULL) { |
30
|
|
|
|
31
|
|
|
// Check if the type of the variables is valid. |
32
|
|
|
if (!is_string($client_id)) { |
33
|
|
|
throw new InvalidArgumentException("The type of the client_id variable is not valid."); |
34
|
|
|
} |
35
|
|
|
if (!is_string($client_secret)) { |
36
|
|
|
throw new InvalidArgumentException("The type of the client_secret variable is not valid."); |
37
|
|
|
} |
38
|
|
|
if (!is_string($code)) { |
39
|
|
|
throw new InvalidArgumentException("The type of the code variable is not valid."); |
40
|
|
|
} |
41
|
|
|
if (($redirect_uri != NULL) && !is_string($redirect_uri)) { |
|
|
|
|
42
|
|
|
throw new InvalidArgumentException("The type of the redirect_uri variable is not valid."); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Set the arguments of the request |
46
|
|
|
$arguments = array( |
47
|
|
|
"client_id" => $client_id, |
48
|
|
|
"client_secret" => $client_secret, |
49
|
|
|
"code" => $code |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
if($redirect_uri != NULL) { |
|
|
|
|
53
|
|
|
$arguments["redirect_uri"] = $redirect_uri; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->setUrl("oauth.access", $arguments); |
57
|
|
|
|
58
|
|
|
// Send the request |
59
|
|
|
try { |
60
|
|
|
$client = new \GuzzleHttp\Client(); |
61
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
62
|
|
|
$response = json_decode( $json_response->getBody() ); |
63
|
|
|
} |
64
|
|
|
catch (RequestException $e) { |
65
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if($response->{'ok'} === FALSE) { |
69
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $json_response->getBody(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|