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 UsersProfile extends AbstractApi |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @param string $user |
24
|
|
|
* @param bool $include_labels |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
public function get($user = NULL, $include_labels = NULL) { |
|
|
|
|
28
|
|
|
|
29
|
|
|
// Check if the type of the variables is valid. |
30
|
|
|
if (!is_string($user) && ($user != NULL)) { |
31
|
|
|
throw new InvalidArgumentException("The type of the user variable is not valid."); |
32
|
|
|
} |
33
|
|
|
if (!is_string($include_labels) && ($include_labels != NULL)) { |
34
|
|
|
throw new InvalidArgumentException("The type of the include_labels variable is not valid."); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Set the arguments of the request |
38
|
|
|
$arguments = array(); |
39
|
|
|
|
40
|
|
|
if ($user != NULL) { |
|
|
|
|
41
|
|
|
$arguments["user"] = $user; |
42
|
|
|
} |
43
|
|
|
if ($include_labels != NULL) { |
44
|
|
|
$arguments["include_labels"] = $include_labels; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->setUrl("users.profile.get", $arguments); |
48
|
|
|
|
49
|
|
|
// Send the request |
50
|
|
|
try { |
51
|
|
|
$client = new \GuzzleHttp\Client(); |
52
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
53
|
|
|
$response = json_decode( $json_response->getBody() ); |
54
|
|
|
} |
55
|
|
|
catch (RequestException $e) { |
56
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if($response->{'ok'} === FALSE) { |
60
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $json_response->getBody(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
* |
72
|
|
|
* @param string $user |
73
|
|
|
* @param string $profile |
74
|
|
|
* @param string $name |
75
|
|
|
* @param string $value |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function set($user = NULL, $profile = NULL, $name = NULL, $value = NULL) { |
|
|
|
|
79
|
|
|
|
80
|
|
|
// Check if the type of the variables is valid. |
81
|
|
|
if (!is_string($user) && ($user != NULL)) { |
82
|
|
|
throw new InvalidArgumentException("The type of the user variable is not valid."); |
83
|
|
|
} |
84
|
|
|
if (!is_string($profile) && ($profile != NULL)) { |
85
|
|
|
throw new InvalidArgumentException("The type of the profile variable is not valid."); |
86
|
|
|
} |
87
|
|
|
if (!is_string($name) && ($name != NULL)) { |
88
|
|
|
throw new InvalidArgumentException("The type of the name variable is not valid."); |
89
|
|
|
} |
90
|
|
|
if (!is_string($value) && ($value != NULL)) { |
91
|
|
|
throw new InvalidArgumentException("The type of the value variable is not valid."); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Set the arguments of the request |
95
|
|
|
$arguments = array(); |
96
|
|
|
|
97
|
|
|
if ($user != NULL) { |
|
|
|
|
98
|
|
|
$arguments["user"] = $user; |
99
|
|
|
} |
100
|
|
|
if ($profile != NULL) { |
|
|
|
|
101
|
|
|
$arguments["profile"] = $profile; |
102
|
|
|
} |
103
|
|
|
if ($name != NULL) { |
|
|
|
|
104
|
|
|
$arguments["name"] = $name; |
105
|
|
|
} |
106
|
|
|
if ($value != NULL) { |
|
|
|
|
107
|
|
|
$arguments["value"] = $value; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->setUrl("users.profile.set", $arguments); |
111
|
|
|
|
112
|
|
|
// Send the request |
113
|
|
|
try { |
114
|
|
|
$client = new \GuzzleHttp\Client(); |
115
|
|
|
$json_response = $client->request('GET', $this->getUrl(), []); |
116
|
|
|
$response = json_decode( $json_response->getBody() ); |
117
|
|
|
} |
118
|
|
|
catch (RequestException $e) { |
119
|
|
|
throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if($response->{'ok'} === FALSE) { |
123
|
|
|
throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $json_response->getBody(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.