Skip to content

Commit a92cf9f

Browse files
committed
wip
1 parent ba668a8 commit a92cf9f

File tree

5 files changed

+323
-10
lines changed

5 files changed

+323
-10
lines changed

src/Client.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Endpoint/Meetings.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace UDX\Zoom\Endpoint;
4+
5+
use UDX\Zoom\Http\Request;
6+
7+
class Meetings extends Request {
8+
9+
public function __construct($apiKey, $apiSecret) {
10+
parent::__construct($apiKey, $apiSecret);
11+
}
12+
13+
/**
14+
* List
15+
*
16+
* @param $userId
17+
* @return array|mixed
18+
*/
19+
public function list(string $userId) {
20+
return $this->get("users/{$userId}/meetings");
21+
}
22+
23+
/**
24+
* Create
25+
*
26+
* @param $userId
27+
* @param array $data
28+
* @return array|mixed
29+
*/
30+
public function create(string $userId, array $data = null) {
31+
return $this->post("users/{$userId}/meetings", $data);
32+
}
33+
34+
/**
35+
* Meeting
36+
*
37+
* @param $meetingId
38+
* @return array|mixed
39+
*/
40+
public function meeting(string $meetingId) {
41+
return $this->get("meetings/{$meetingId}");
42+
}
43+
44+
/**
45+
* Records
46+
*
47+
* @param $meetingId
48+
* @return array|mixed
49+
*/
50+
public function records(string $meetingId) {
51+
return $this->get("meetings/{$meetingId}/recordings");
52+
}
53+
54+
}

src/Http/Request.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
namespace UDX\Zoom\Http;
4+
5+
use Firebase\JWT\JWT;
6+
use GuzzleHttp\Client;
7+
use GuzzleHttp\Exception\ClientException;
8+
use GuzzleHttp\Psr7\Response;
9+
10+
class Request {
11+
12+
/**
13+
* @var
14+
*/
15+
protected $apiKey;
16+
17+
/**
18+
* @var
19+
*/
20+
protected $apiSecret;
21+
22+
/**
23+
* @var Client
24+
*/
25+
protected $client;
26+
27+
/**
28+
* @var string
29+
*/
30+
public $apiPoint = 'https://api.zoom.us/v2/';
31+
32+
/**
33+
* Request constructor.
34+
* @param $apiKey
35+
* @param $apiSecret
36+
*/
37+
public function __construct( $apiKey, $apiSecret ) {
38+
$this->apiKey = $apiKey;
39+
40+
$this->apiSecret = $apiSecret;
41+
42+
$this->client = new Client();
43+
}
44+
45+
/**
46+
* Headers
47+
*
48+
* @return array
49+
*/
50+
protected function headers(): array
51+
{
52+
return [
53+
'Authorization' => 'Bearer ' . $this->generateJWT(),
54+
'Content-Type' => 'application/json',
55+
'Accept' => 'application/json',
56+
];
57+
}
58+
59+
/**
60+
* Generate J W T
61+
*
62+
* @return string
63+
*/
64+
protected function generateJWT() {
65+
$token = [
66+
'iss' => $this->apiKey,
67+
'exp' => time() + 60,
68+
];
69+
70+
return JWT::encode($token, $this->apiSecret);
71+
}
72+
73+
74+
/**
75+
* Get
76+
*
77+
* @param $method
78+
* @param array $fields
79+
* @return array|mixed
80+
*/
81+
protected function get($method, $fields = [])
82+
{
83+
try {
84+
$response = $this->client->request('GET', $this->apiPoint . $method, [
85+
'query' => $fields,
86+
'headers' => $this->headers(),
87+
]);
88+
89+
return $this->result($response);
90+
91+
} catch (ClientException $e) {
92+
93+
return (array)json_decode($e->getResponse()->getBody()->getContents());
94+
}
95+
}
96+
97+
/**
98+
* Post
99+
*
100+
* @param $method
101+
* @param $fields
102+
* @return array|mixed
103+
*/
104+
protected function post($method, $fields)
105+
{
106+
$body = \json_encode($fields, JSON_PRETTY_PRINT);
107+
108+
try {
109+
$response = $this->client->request('POST', $this->apiPoint . $method,
110+
['body' => $body, 'headers' => $this->headers()]);
111+
112+
return $this->result($response);
113+
114+
} catch (ClientException $e) {
115+
116+
return (array)json_decode($e->getResponse()->getBody()->getContents());
117+
}
118+
}
119+
120+
/**
121+
* Patch
122+
*
123+
* @param $method
124+
* @param $fields
125+
* @return array|mixed
126+
*/
127+
protected function patch($method, $fields)
128+
{
129+
$body = \json_encode($fields, JSON_PRETTY_PRINT);
130+
131+
try {
132+
$response = $this->client->request('PATCH', $this->apiPoint . $method,
133+
['body' => $body, 'headers' => $this->headers()]);
134+
135+
return $this->result($response);
136+
137+
} catch (ClientException $e) {
138+
139+
return (array)json_decode($e->getResponse()->getBody()->getContents());
140+
}
141+
}
142+
143+
protected function delete($method)
144+
{
145+
try {
146+
$response = $this->client->request('DELETE', $this->apiPoint . $method,
147+
[ 'headers' => $this->headers()]);
148+
149+
return $this->result($response);
150+
151+
} catch (ClientException $e) {
152+
153+
return (array)json_decode($e->getResponse()->getBody()->getContents());
154+
}
155+
}
156+
157+
/**
158+
* Result
159+
*
160+
* @param Response $response
161+
* @return mixed
162+
*/
163+
protected function result(Response $response)
164+
{
165+
$result = json_decode((string)$response->getBody(), true);
166+
167+
$result['code'] = $response->getStatusCode();
168+
169+
return $result;
170+
}
171+
}

src/Zoom.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace UDX\Zoom;
4+
use Exception;
5+
6+
class Zoom {
7+
8+
/**
9+
* @var null
10+
*/
11+
private $apiKey = null;
12+
13+
/**
14+
* @var null
15+
*/
16+
private $apiSecret = null;
17+
18+
/**
19+
* Zoom constructor.
20+
* @param $apiKey
21+
* @param $apiSecret
22+
*/
23+
public function __construct( $apiKey, $apiSecret ) {
24+
25+
$this->apiKey = $apiKey;
26+
27+
$this->apiSecret = $apiSecret;
28+
}
29+
30+
/**
31+
* __call
32+
*
33+
* @param $method
34+
* @param $args
35+
* @return mixed
36+
*/
37+
public function __call( $method, $args ) {
38+
return $this->make( $method );
39+
}
40+
41+
/**
42+
* __get
43+
*
44+
* @param $name
45+
* @return mixed
46+
*/
47+
public function __get( $name ) {
48+
return $this->make( $name );
49+
}
50+
/**
51+
* Make
52+
*
53+
* @param $resource
54+
* @return mixed
55+
* @throws Exception
56+
*/
57+
public function make( $resource ) {
58+
59+
$class = 'UDX\\Zoom\\Endpoint\\' . ucfirst(strtolower($resource));
60+
if (class_exists($class)) {
61+
return new $class( $this->apiKey, $this->apiSecret );
62+
}
63+
throw new Exception('Wrong method');
64+
}
65+
}

tests/Bootstrap.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
11
<?php
22

33
use PHPUnit\Framework\TestCase;
4+
use UDX\Zoom;
45

6+
/**
7+
* Class Bootstrap
8+
*/
59
class Bootstrap extends TestCase {
610

7-
public function testClient() {
8-
$client = new \UDX\Zoom\Client();
9-
$this->assertInstanceOf(\UDX\Zoom\Client::class, $client);
11+
/**
12+
* Test Client Create
13+
*
14+
*/
15+
public function testClientCreate() {
16+
try {
17+
$client = new Zoom\Zoom('key', 'secret');
18+
$this->assertInstanceOf(Zoom\Zoom::class, $client);
19+
} catch ( Exception $e ) {
20+
$this->fail( $e );
21+
}
22+
}
23+
24+
/**
25+
* Test Meetings Endpoint
26+
*
27+
*/
28+
public function testMeetingsEndpoint() {
29+
try {
30+
$client = new Zoom\Zoom( 'key', 'secret' );
31+
$this->assertInstanceOf(Zoom\Endpoint\Meetings::class, $client->meetings);
32+
$this->assertInstanceOf(Zoom\Http\Request::class, $client->meetings);
33+
$this->assertTrue( method_exists( $client->meetings, 'list' ) );
34+
$this->assertTrue( method_exists( $client->meetings, 'create' ) );
35+
$this->assertTrue( method_exists( $client->meetings, 'meeting' ) );
36+
$this->assertTrue( method_exists( $client->meetings, 'records' ) );
37+
} catch ( Exception $e ) {
38+
$this->fail( $e );
39+
}
1040
}
1141
}

0 commit comments

Comments
 (0)