Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
declare(strict_types=1);
3
 
4
 
5
namespace LeadersLinked\Library;
6
 
7
 
8
use \Firebase\JWT\JWT;
9
use GuzzleHttp\Client;
10
 
11
use Laminas\Db\Adapter\Adapter;
12
use LeadersLinked\Model\ZoomMeeting;
13
use LeadersLinked\Cache\CacheInterface;
14
use LeadersLinked\Cache\CacheImpl;
15
 
16
 
17
class Zoom
18
{
19
    const CACHE_OAUTH_TOOKEN = 'zoom_oauth_tooken';
20
 
21
    /**
22
     *
23
     * @var Adapter
24
     */
25
    private $adapter;
26
 
27
 
28
    /**
29
     *
30
     * @var array
31
     */
32
    private $config;
33
 
34
    /**
35
     *
36
     * @var CacheInterface
37
     */
38
    private $cache;
39
 
40
    /**
41
     *
42
     * @param Adapter $adapter
43
     * @param array config
44
     * @param CacheInterface $cache
45
     */
46
    public function __construct($adapter, $config, $cache)
47
    {
48
        $this->adapter = $adapter;
49
        $this->config = $config;
50
        $this->cache = $cache;
51
    }
52
 
53
 
54
 
55
    public function addMeeting($access_token, $topic, $agenda, $type, $start_time, $duration, $timezone, $password)
56
    {
57
 
58
        $base_url = $this->config['leaderslinked.zoom.base_url'];
59
 
60
 
61
 
62
        $client = new Client([
63
            'base_uri' => $base_url,
64
        ]);
65
 
66
 
67
 
68
        if($type == ZoomMeeting::TYPE_SCHEDULED) {
69
            $json = [
70
                'topic' => $topic,
71
                'agenda' => $agenda,
72
                'type' => 2,
73
                'start_time' => $start_time,
74
                'duration' => $duration,
75
                'password' => $password,
76
                'timezone' => $timezone,
77
                'settings' => [
78
                    'participant_video'=> 'true',
79
                    'auto_recording' => 'true',
80
                ]
81
            ];
82
        } else {
83
            $json = [
84
                'topic' => $topic,
85
                'agenda' => $agenda,
86
                'type' => 1,
87
                'duration' => $duration,
88
                'password' => $password,
89
                'timezone' => $timezone,
90
                'settings' => [
91
                    'participant_video'=> 'true',
92
                    'auto_recording' => 'none',
93
                ]
94
            ];
95
        }
96
 
97
 
98
        $request = [
99
            'headers' => [
100
                'Authorization' => 'Bearer ' . $access_token
101
            ],
102
            'json' => $json,
103
        ];
104
 
105
 
106
        $response = $client->request('POST', '/v2/users/me/meetings', $request );
107
        $statusCode = $response->getStatusCode() ;
108
 
109
       // error_log('$statusCode = ' . $statusCode);
110
 
111
 
112
 
113
        if($statusCode >= 200 && $statusCode <= 300) {
114
            $data = json_decode($response->getBody()->getContents());
115
 
116
 
117
            return [
118
                'success' => true,
119
                'data' => [
120
                    'id' => $data->id,
121
                    'uuid' => $data->uuid,
122
                    'join_url' => $data->join_url,
123
                    'password' => $data->password,
124
                ]
125
 
126
            ];
127
 
128
 
129
        } else {
130
            return [
131
                'success' => false,
132
                'data' => $response->getStatusCode()
133
            ];
134
        }
135
 
136
 
137
 
138
    }
139
 
140
    public function getMeetings($access_token, $next_page_token)
141
    {
142
        $base_url = $this->config['leaderslinked.zoom.base_url'];
143
        $client = new Client([
144
 
145
            'base_uri' => $base_url,
146
        ]);
147
 
148
        $request = [
149
            'headers' => [
150
                'Authorization' => 'Bearer '. $access_token
151
            ],
152
            'type' => 'scheduled'
153
        ];
154
 
155
        if($next_page_token) {
156
            $request['query'] = ['next_page_token' => $next_page_token];
157
        }
158
 
159
 
160
        $response = $client->request('GET', '/v2/users/me/meetings', $request);
161
        $statusCode = $response->getStatusCode() ;
162
 
163
        if($statusCode >= 200 && $statusCode <= 300) {
164
            $meetings = json_decode($response->getBody()->getContents());
165
 
166
            return [
167
                'success' =>  true,
168
                'data' => $meetings
169
            ];
170
 
171
        } else {
172
            return [
173
                'success' => false,
174
                'data' => $response->getStatusCode()
175
            ];
176
        }
177
    }
178
 
179
 
180
 
181
 
182
 
183
    public  function getOAuthAccessToken()
184
    {
185
 
186
        $data = $this->cache->getItem(self::CACHE_OAUTH_TOOKEN);
187
        if($data) {
188
 
189
            return [
190
                'success' => true,
191
                'data' => $data->accesstoken
192
 
193
            ];
194
        }
195
 
196
 
197
 
198
        $account_id     = $this->config['leaderslinked.zoom.account_id'];
199
        $client_id      = $this->config['leaderslinked.zoom.client_id'];
200
        $client_secret  = $this->config['leaderslinked.zoom.client_secret'];
201
 
202
        $client_token_base64_encode =  base64_encode($client_id . ':' . $client_secret);
203
 
204
 
205
        $timecalled = time();
206
 
207
        $client = new Client([
208
            'base_uri' => 'https://zoom.us',
209
        ]);
210
 
211
        $request = [
212
            'headers' => [
213
                'Authorization' => 'Basic ' . $client_token_base64_encode,
214
                'Accept' =>  'application/json'
215
            ],
216
            'form_params' => [
217
                'grant_type' => 'account_credentials',
218
                'account_id' => $account_id
219
            ],
220
        ];
221
 
222
 
223
        $response = $client->request('POST', '/oauth/token', $request );
224
        $statusCode = $response->getStatusCode() ;
225
 
226
 
227
        if($statusCode >= 200 && $statusCode <= 300) {
228
            $response = json_decode($response->getBody()->getContents());
229
 
230
            $requiredscopes = [
231
                'meeting:read:admin',
232
                'meeting:write:admin',
233
                'user:read:admin',
234
            ];
235
            $scopes = explode(' ', $response->scope);
236
            $missingscopes = array_diff($requiredscopes, $scopes);
237
 
238
            if (!empty($missingscopes)) {
239
                $missingscopes = implode(', ', $missingscopes);
240
 
241
 
242
                return [
243
                    'success' => false,
244
                    'data' => 'ERROR_OAUTH_MISSING_SCOPES ' . $missingscopes
245
                ];
246
 
247
 
248
 
249
 
250
            }
251
 
252
            if (isset($response->expires_in)) {
253
                $expires = $response->expires_in + $timecalled;
254
            } else {
255
                $expires = 3599 + $timecalled;
256
            }
257
 
258
            $obj = new \stdClass();
259
            $obj->accesstoken   = $response->access_token;
260
            $obj->expires       = $expires;
261
            $obj->scopes        = $scopes;
262
 
263
            $this->cache->setItem(self::CACHE_OAUTH_TOOKEN, $obj);
264
 
265
            return [
266
                'success' => true,
267
                'data' => $response->access_token
268
            ];
269
 
270
 
271
        } else {
272
            return [
273
                'success' => false,
274
                'data' => $response->getStatusCode()
275
            ];
276
 
277
 
278
            return $obj;
279
 
280
        }
281
    }
282
 
283
 
284
}