|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenapply\Calendly; |
|
4
|
|
|
|
|
5
|
|
|
use Zenapply\Calendly\Exceptions\CalendlyException; |
|
6
|
|
|
use Zenapply\Request\CurlRequest; |
|
7
|
|
|
use Zenapply\Request\HttpRequest; |
|
8
|
|
|
|
|
9
|
|
|
class Calendly { |
|
10
|
|
|
const V1 = "v1"; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* API Host |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $host; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* API Token |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $token; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* API Version to use |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $version; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The HttpRequest instance that will handle the request |
|
32
|
|
|
* @var HttpRequest |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $request; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Creates a Calendly instance that can register and unregister webhooks with the API |
|
38
|
|
|
* @param string $token The API token to use |
|
39
|
|
|
* @param string $version The API version to use |
|
40
|
|
|
* @param string $host The Host URL |
|
41
|
|
|
* @param string $request The HttpRequest instance that will handle the request |
|
42
|
|
|
*/ |
|
43
|
24 |
|
public function __construct($token,$version = self::V1,$host = "calendly.com", HttpRequest $request = null){ |
|
44
|
24 |
|
$this->request = $request; |
|
45
|
24 |
|
$this->token = $token; |
|
46
|
24 |
|
$this->version = $version; |
|
47
|
24 |
|
$this->host = $host; |
|
48
|
24 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Will register a webhook url for the both invitee events |
|
52
|
|
|
* @param string $url The webhook url you want to use |
|
53
|
|
|
* @return array The response array |
|
54
|
|
|
*/ |
|
55
|
|
|
public function registerAllInviteeEvents($url){ |
|
56
|
|
|
return $this->register(['url'=>$url,'events'=>['invitee.created','invitee.canceled']]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Will register a webhook url for the invitee.created event |
|
61
|
|
|
* @param string $url The webhook url you want to use |
|
62
|
|
|
* @return array The response array |
|
63
|
|
|
*/ |
|
64
|
3 |
|
public function registerInviteeCreated($url){ |
|
65
|
3 |
|
return $this->register(['url'=>$url,'events'=>['invitee.created']]); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Will register a webhook url for the invitee.canceled event |
|
70
|
|
|
* @param string $url The webhook url you want to use |
|
71
|
|
|
* @return array The response array |
|
72
|
|
|
*/ |
|
73
|
3 |
|
public function registerInviteeCanceled($url){ |
|
74
|
3 |
|
return $this->register(['url'=>$url,'events'=>['invitee.canceled']]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Sends a request to delete a hook |
|
79
|
|
|
* @param integer|string $id The ID of the hook you want to delete |
|
80
|
|
|
* @return null returns null when successful |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function unregister($id){ |
|
83
|
3 |
|
$url = $this->buildUrl("hooks") . "/" . $id; |
|
84
|
3 |
|
return $this->exec($url,null,"DELETE"); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Sends a request to create a hook with the provided data |
|
89
|
|
|
* @param array $data The data you want to include in the POST request |
|
90
|
|
|
* @return array The response array |
|
91
|
|
|
*/ |
|
92
|
6 |
|
protected function register($data){ |
|
93
|
6 |
|
$url = $this->buildUrl("hooks"); |
|
94
|
6 |
|
$data = $this->buildPostFields($data); |
|
95
|
6 |
|
return $this->exec($url,$data); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Converts an array into a post fields string for CURL |
|
100
|
|
|
* @param array $data The data you want to convert |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
12 |
|
protected function buildPostFields(array $data){ |
|
104
|
12 |
|
$data = http_build_query($data); |
|
105
|
12 |
|
return preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $data); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Builds the URL from the host and version properties |
|
110
|
|
|
* @param string $action The API action you want to execute |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
12 |
|
protected function buildUrl($action = "hooks"){ |
|
114
|
12 |
|
return "https://{$this->host}/api/{$this->version}/{$action}"; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns the HttpRequest instance |
|
119
|
|
|
* @param string $url The URL to request |
|
120
|
|
|
* @return HttpRequest |
|
121
|
|
|
*/ |
|
122
|
9 |
|
protected function getRequest($url){ |
|
123
|
9 |
|
$request = $this->request; |
|
124
|
9 |
|
if(!$request instanceof HttpRequest){ |
|
125
|
|
|
$request = new CurlRequest($url); |
|
126
|
|
|
} |
|
127
|
9 |
|
return $request; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Executes a CURL command to the Calendly API |
|
132
|
|
|
* @param string $url The URL to send to |
|
133
|
|
|
* @param string $data Data from the http_build_query function |
|
134
|
|
|
* @param string $method POST, GET, DELETE, PUT, etc... |
|
135
|
|
|
* @return array The response data as an assoc array |
|
136
|
|
|
*/ |
|
137
|
9 |
|
protected function exec($url = "", $data = null, $method = "POST"){ |
|
138
|
9 |
|
$request = $this->getRequest($url); |
|
139
|
|
|
|
|
140
|
9 |
|
$request->setOptionArray([ |
|
141
|
9 |
|
CURLOPT_CUSTOMREQUEST => $method, |
|
142
|
9 |
|
CURLOPT_HTTPHEADER => [ |
|
143
|
9 |
|
"X-Token: {$this->token}" |
|
144
|
9 |
|
], |
|
145
|
9 |
|
CURLOPT_URL => $url, |
|
146
|
9 |
|
CURLOPT_POSTFIELDS => $data, |
|
147
|
9 |
|
CURLOPT_VERBOSE => false, |
|
148
|
9 |
|
CURLOPT_HEADER => false, |
|
149
|
9 |
|
CURLOPT_RETURNTRANSFER => true, |
|
150
|
9 |
|
]); |
|
151
|
|
|
|
|
152
|
9 |
|
$result = $request->execute(); |
|
153
|
9 |
|
$code = $request->getInfo(CURLINFO_HTTP_CODE); |
|
154
|
|
|
|
|
155
|
9 |
|
$request->close(); |
|
156
|
|
|
|
|
157
|
9 |
|
return $this->handleResponse($result,$code); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Returns the response as an array or throws an Exception if it was unsuccessful |
|
162
|
|
|
* @param string $result JSON string from the response |
|
163
|
|
|
* @param integer $code The HTTP response code |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
15 |
|
protected function handleResponse($result,$code){ |
|
167
|
15 |
|
$data = json_decode($result, true); |
|
168
|
15 |
|
if($code>=200 && $code<300) { |
|
169
|
12 |
|
return $data; |
|
170
|
|
|
} else { |
|
171
|
3 |
|
throw new CalendlyException($data['message']); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
} |