MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/www/mpl.imenso.co/app/Models/PushNotification.php
<?php

namespace App\Models;

use LaravelFCM\Facades\FCM;
use LaravelFCM\Message\OptionsBuilder;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;

class PushNotification
{
    public $title = '';
    public $body = '';
    public $badge_count = 0;
    public $sound = 'default';
    public $abandon_after = 1800;
    public $data;
    private $options_builder;
    private $notification_payload;
    private $data_payload;
    private $recipient_tokens = [];

    public static function create()
    {
        $notification = new self;

        return $notification;
    }

    public function abandonAfter($seconds = 0)
    {
        $this->abandon_after = $seconds;

        return $this;
    }

    public function withTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    public function withBody($body)
    {
        $this->body = $body;

        return $this;
    }

    public function withSound($sound)
    {
        $this->sound = $sound;

        return $this;
    }

    public function withData($data)
    {
        $this->data = $data;

        return $this;
    }

    public function withBadgeCount($count = 0)
    {
        $this->badge_count = $count;

        return $this;
    }

    private function createOptionsBuilder()
    {
        $this->option_builder = new OptionsBuilder();
        $this->option_builder->setTimeToLive(60 * 20);

        return $this->option_builder->build();
    }

    private function createNotificationPayload()
    {
        $this->notification_payload = new PayloadNotificationBuilder($this->title);

        if ($this->body) {
            $this->notification_payload->setBody($this->body);
        }

        if ($this->sound) {
            $this->notification_payload->setSound($this->sound);
        }

        $this->notification_payload->setBadge($this->badge_count);

        return $this->notification_payload->build();
    }

    private function createDataPayload()
    {
        if (is_array($this->data)) {
            $this->data_payload = new PayloadDataBuilder();
            $this->data_payload->addData($this->data);

            return $this->data_payload->build();
        }
    }

    public function toToken($token)
    {
        $this->recipient_tokens[] = $token;

        return $this;
    }

    public function toTokens($tokens)
    {
        foreach ($tokens as $token) {
            $this->recipient_tokens[] = $token;
        }

        return $this;
    }

    public function clearTokens()
    {
        $this->recipient_tokens = [];
    }

    public function deleteTokens($tokens)
    {
        foreach (Device::whereIn('fcm_token', $tokens)->get() as $device) {
            $device->clearFCMToken();
        }
    }

    public function modifyTokens($tokens)
    {
        foreach ($tokens as $old => $new) {
            $device = Device::where('fcm_token', $old)->first();
            $device->updateFCMToken($new);
        }
    }

    public function retryTokens($tokens)
    {
        $this->toTokens($tokens);
        $this->send();
    }

    public function send()
    {
        if (is_null(config('fcm.http.server_key')) && is_null(config('fcm.http.server_id'))) {
            return;
        }

        $option = $this->createOptionsBuilder();
        $notification = $this->createNotificationPayload();
        $data = $this->createDataPayload();

        $response = FCM::sendTo($this->recipient_tokens, $option, $notification, $data);

        $this->clearTokens();

        if ($response->tokensToDelete()) {
            $this->deleteTokens($response->tokensToDelete());
        }

        if ($response->tokensToModify()) {
            $this->modifyTokens($response->tokensToModify());
        }

        if ($response->tokensToRetry()) {
            $this->retryTokens($response->tokensToRetry());
        }
    }
}