hooman-mirghasemi/Laravel-iran-sms

View on GitHub
src/Listeners/DbLogListener.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace HoomanMirghasemi\Sms\Listeners;

use HoomanMirghasemi\Sms\Abstracts\Driver;
use HoomanMirghasemi\Sms\Contracts\SmsEvent;
use HoomanMirghasemi\Sms\Models\SmsReport;

class DbLogListener
{
    /**
     * Handle the event.
     *
     * @param object $event
     *
     * @return void
     */
    public function handle(SmsEvent $event): void
    {
        $this->saveLogInDb($event->smsDriver);
    }

    /**
     * This method should call in all driver send method in last line.
     *
     * @return void
     */
    protected function saveLogInDb(Driver $smsDriver): void
    {
        $sendSmsReport = new SmsReport();
        $sendSmsReport->mobile = $smsDriver->getRecipient();
        $sendSmsReport->message = $smsDriver->getMessage();
        $reflect = new \ReflectionClass($smsDriver);
        $sendSmsReport->from = strtolower($reflect->getShortName());
        $sendSmsReport->number = $smsDriver->getSenderNumber();
        $sendSmsReport->web_service_response = $smsDriver->getWebServiceResponce();
        $sendSmsReport->success = $smsDriver->getResult();
        $sendSmsReport->save();
    }
}