%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/camillo/camillo-api-master/app/Jobs/
Upload File :
Create Path :
Current File : /var/www/html/camillo/camillo-api-master/app/Jobs/ChatRoomCleaner.php

<?php

namespace App\Jobs;

use App\Enums\UsertypeEnum;
use App\Models\Classroom;
use App\User;
use Chatkit\Chatkit;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;

class ChatRoomCleaner implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $chatkit;

    /**
     * Create a new job instance.
     *
     * @throws \Chatkit\Exceptions\MissingArgumentException
     */
    public function __construct()
    {
        $this->chatkit = new Chatkit([
            'instance_locator' => env('PUSHER_APP_KEY'),
            'key'              => env('PUSHER_APP_SECRET'),
        ]);
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        /** @var Classroom $classroom */
        foreach (Classroom::whereNotNull('pusher_id')->get() as $classroom) {
            $room = null;
            try {
                $room = $this->chatkit->getRoom(['id' => $classroom->pusher_id]);
                if ($room) {
                    foreach ($room['body']['member_user_ids'] as $userId) {
                        $user = User::where('pusher_id', $userId)->first();
                        if ($user && $user->usertype->role !== UsertypeEnum::TEACHER) {
                            $individual = $user->individual;
                            // se non esiste un minore associato allo user nella classroom rimuovo l'utente dalla room
                            if ($classroom->minors()->where('minors.active', true)->whereHas('individuals', function ($i) use ($individual) {
                                    $i->where('id', $individual->id);
                                })->count() === 0) {
                                $this->chatkit->removeUsersFromRoom([
                                    'user_ids' => [$userId],
                                    'room_id'  => $classroom->pusher_id,
                                ]);
                            }
                        }
                    }
                }
            } catch (\Exception $exception) {
                continue;
            }
        }
    }
}

Zerion Mini Shell 1.0