%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/klinisol/klinisol-api/app/Services/
Upload File :
Create Path :
Current File : /var/www/html/klinisol/klinisol-api/app/Services/KlinisolChat.php

<?php

namespace App\Services;


use App\Events\ChatRoomCreatedEvent;
use App\Models\Patient;
use App\Models\Protocol;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use phpDocumentor\Reflection\Types\Boolean;
use Twilio\Exceptions\RestException;
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VideoGrant;
use Twilio\Rest\Client;

class KlinisolChat
{
    public static function generateToken(User $user, $room)
    {
        /** @var AccessToken $token */
        $token = new AccessToken(env('TWILIO_SID'), env('TWILIO_VIDEO_SID'), env('TWILIO_VIDEO_SECRET'));
        $grant = new VideoGrant();
        $token->addGrant($grant);
        $grant->setRoom($room);
        $identity = $user->fullName();
        $token->setIdentity($identity);
        return [
            'identity' => $identity,
            'token'    => $token->toJWT(),
        ];
    }

    public static function generatePatientToken(Request $request, Patient $patient, $protocol, bool $hasVideo)
    {
        /** @var Protocol $protocol */
        $protocol = Protocol::query()
                            ->find($protocol);
        /** @var AccessToken $token */
        $token = new AccessToken(env('TWILIO_SID'), env('TWILIO_VIDEO_SID'), env('TWILIO_VIDEO_SECRET'));
        $grant = new VideoGrant();
        if ($hasVideo) {
            $roomName = 'V-' . $patient->code . '-' . $protocol->code;
        } else {
            $roomName = 'A-' . $patient->code . '-' . $protocol->code;
        }
        $roomName = str_replace(' ', '-', $roomName);
        $department = $request->header('Department');
        $room = self::createNewRoom($roomName, $department);
        $grant->setRoom($room->sid);
        $token->addGrant($grant);
        $identity = $patient->fullName();
        $token->setIdentity($identity);

        $admins = User::getAdminChannels();
        event(new ChatRoomCreatedEvent($roomName, $admins, $department));

        return [
            'identity'  => $identity,
            'room_name' => $roomName,
            'room_sid'  => $room->sid,
            'token'     => $token->toJWT(),
        ];
    }

    public static function createNewRoom($roomName, $department)
    {
        $sid = env("TWILIO_SID");
        $token = env("TWILIO_TOKEN");
        $twilio = new Client($sid, $token);
        try {
            $room = $twilio->video->v1->rooms->create([
                "uniqueName"     => $roomName,
                "StatusCallback" => str_replace("http://", "https://", route("api.v1.webhook.twilio.video")) . "?department=$department",
            ]);
        } catch (RestException $e) {
            $room = $twilio->video->v1->rooms($roomName)
                                      ->fetch();
        }
        return $room;
    }

}

Zerion Mini Shell 1.0