Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16769 | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?php
declare(strict_types=1);


namespace LeadersLinked\Library;


use \Firebase\JWT\JWT;
use GuzzleHttp\Client;

use Laminas\Db\Adapter\Adapter;
use LeadersLinked\Model\ZoomMeeting;
use LeadersLinked\Cache\CacheInterface;
use LeadersLinked\Cache\CacheImpl;


class Zoom 
{
    const CACHE_OAUTH_TOOKEN = 'zoom_oauth_tooken';
    
    /**
     * 
     * @var Adapter
     */
    private $adapter; 
    
    
    /**
     *
     * @var array
     */
    private $config;
    
    /**
     * 
     * @var CacheInterface
     */
    private $cache;
    
    
    
    
    /**
     * 
     * @param Adapter $adapter
     * @param array config
     * @param CacheInterface $cache
     */
    public function __construct($adapter, $config, $cache)
    {
        $this->adapter = $adapter;
        $this->config = $config;
        $this->cache = $cache;
    }
    
    
    
    public function addMeeting($access_token, $topic, $agenda, $type, $start_time, $duration, $timezone, $password)
    {

        $base_url = $this->config['leaderslinked.zoom.base_url'];

            

        $client = new Client([
            'base_uri' => $base_url,
        ]);
        


        if($type == ZoomMeeting::TYPE_SCHEDULED) {
            $json = [
                'topic' => $topic,
                'agenda' => $agenda,
                'type' => 2,
                'start_time' => $start_time,
                'duration' => $duration, 
                'password' => $password,
                'timezone' => $timezone,
                'settings' => [
                    'participant_video'=> 'true',
                    'auto_recording' => 'true',
                ]
            ];
        } else {
            $json = [
                'topic' => $topic,
                'agenda' => $agenda,
                'type' => 1,
                'duration' => $duration,
                'password' => $password,
                'timezone' => $timezone,
                'settings' => [
                    'participant_video'=> 'true',
                    'auto_recording' => 'none',
                ]
            ];
        }
    
        
        $request = [
            'headers' => [
                'Authorization' => 'Bearer ' . $access_token
            ],
            'json' => $json,
        ];
        
            
        $response = $client->request('POST', '/v2/users/me/meetings', $request );
        $statusCode = $response->getStatusCode() ;
        
       // error_log('$statusCode = ' . $statusCode);
        
       
        
        if($statusCode >= 200 && $statusCode <= 300) {
            $data = json_decode($response->getBody()->getContents());

            
            return [
                'success' => true, 
                'data' => [
                    'id' => $data->id,
                    'uuid' => $data->uuid,
                    'join_url' => $data->join_url,
                    'password' => $data->password,
                ]
                
            ];
            
            
        } else {
            return [
                'success' => false, 
                'data' => $response->getStatusCode()
            ]; 
        }
            
       
        
    }
    
    public function getMeetings($access_token, $next_page_token)
    {
        $base_url = $this->config['leaderslinked.zoom.base_url'];
        $client = new Client([
            
            'base_uri' => $base_url,
        ]);
        
        $request = [
            'headers' => [
                'Authorization' => 'Bearer '. $access_token
            ],
            'type' => 'scheduled'
        ];
        
        if($next_page_token) {
            $request['query'] = ['next_page_token' => $next_page_token];
        }
        
        
        $response = $client->request('GET', '/v2/users/me/meetings', $request);
        $statusCode = $response->getStatusCode() ;
        
        if($statusCode >= 200 && $statusCode <= 300) {
            $meetings = json_decode($response->getBody()->getContents());
            
            return [
                'success' =>  true,
                'data' => $meetings
            ];
            
        } else {
            return [
                'success' => false,
                'data' => $response->getStatusCode()
            ];
        }
    }
    
    

    
    
    public  function getOAuthAccessToken()
    {


        if($this->cache->hasItem(self::CACHE_OAUTH_TOOKEN)) {
            $data = $this->cache->getItem(self::CACHE_OAUTH_TOOKEN);
            
            return [
                'success' => true,
                'data' => $data->accesstoken
                
            ];
        }
        
        
        
        $account_id     = $this->config['leaderslinked.zoom.account_id'];
        $client_id      = $this->config['leaderslinked.zoom.client_id'];
        $client_secret  = $this->config['leaderslinked.zoom.client_secret'];

        $client_token_base64_encode =  base64_encode($client_id . ':' . $client_secret);
        
        
        $timecalled = time();
        
        $client = new Client([
            'base_uri' => 'https://zoom.us',
        ]);
        
        $request = [
            'headers' => [
                'Authorization' => 'Basic ' . $client_token_base64_encode,
                'Accept' =>  'application/json'
            ],
            'form_params' => [
                'grant_type' => 'account_credentials', 
                'account_id' => $account_id
            ],
        ];
        
        
        $response = $client->request('POST', '/oauth/token', $request );
        $statusCode = $response->getStatusCode() ;

        
        if($statusCode >= 200 && $statusCode <= 300) {
            $response = json_decode($response->getBody()->getContents());

            $requiredscopes = [
                'meeting:read:admin',
                'meeting:write:admin',
                'user:read:admin',
            ];
            $scopes = explode(' ', $response->scope);
            $missingscopes = array_diff($requiredscopes, $scopes);
            
            if (!empty($missingscopes)) {
                $missingscopes = implode(', ', $missingscopes);

                
                return [
                    'success' => false,
                    'data' => 'ERROR_OAUTH_MISSING_SCOPES ' . $missingscopes
                ];


                
                
            }


            
            if (isset($response->expires_in)) {
                $expires = $response->expires_in + $timecalled;
            } else {
                $expires = 3599 + $timecalled;
            }
            
            $obj = new \stdClass();
            $obj->accesstoken   = $response->access_token;
            $obj->expires       = $expires;
            $obj->scopes        = $scopes;
            
            $this->cache->setItem(self::CACHE_OAUTH_TOOKEN, $obj);

            return [
                'success' => true,
                'data' => $response->access_token
            ];
            
            
        } else {
            return [
                'success' => false,
                'data' => $response->getStatusCode()
            ];
            
            
            return $obj;

        }
    }
    
    
}