UpdateVoiceCallStatusCommand.php 2.76 KB
<?php

namespace NexmoBundle\Event\Logger\Command;

use AppBundle\Entity\VoiceCall;
use Chromedia\AuditLogBundle\Entity\Action;
use Chromedia\AuditLogBundle\Entity\ObjectName;
use Chromedia\AuditLogBundle\Entity\ObjectType;
use NexmoBundle\Event\Logger\VoiceCallAuditLogCommand;
use NexmoBundle\VoiceCall\VoiceCallStatus;

class UpdateVoiceCallStatusCommand extends VoiceCallAuditLogCommand
{

    public function isProcessable()
    {
        $knownStatus = [
            VoiceCallStatus::STARTED,
            VoiceCallStatus::COMPLETED,
            VoiceCallStatus::FAILED,
            VoiceCallStatus::BUSY
        ];

        return parent::isProcessable() && \in_array($this->voiceCallLog->getStatus(), $knownStatus);
    }

    protected function buildLogData()
    {
        $actor = $this->voiceCallLog->getActor();
        $data = [
            // to show in patient logs, set object id to patient, object type account
            "objectId" => $actor ? $actor->getId() : "Unknown",
            "objectType" => ObjectType::ACCOUNT,
            "objectName" => ObjectName::VOICE_CALL,
            "actionStatus" => Action::STATUS_SUCCESS,
            "subject" => $this->voiceCallLog->getActor()
                ? $this->voiceCallLog->getActor()->getProfile()->getFullName()
                : "Anonymous",
        ];

        $recipientName = $actor
            ? $this->voiceCallLog->getActor()->getProfile()->getFullName()
            : "Unknown";

        $statusHandlers = [
            VoiceCallStatus::STARTED => function () use (&$data, $recipientName) {
                $data["description"] = "started a voice call with {$recipientName}.";
                $data["action"] = Action::STATUS_SUCCESS;
            },
            VoiceCallStatus::COMPLETED => function () use (&$data, $recipientName) {
                $data["description"] = "completed a voice call with {$recipientName} ({$this->voiceCallLog->getFormattedDuration()}). ";
                $data["action"] = Action::STATUS_SUCCESS;
            },
            VoiceCallStatus::FAILED => function () use (&$data, $recipientName) {
                $data["description"] = "failed to complete a voice call with {$recipientName} ({$this->voiceCallLog->getFormattedDuration()}).";
                $data["action"] = Action::STATUS_FAILURE;
            },
            VoiceCallStatus::BUSY => function () use (&$data, $recipientName) {
                $data["description"] = "failed to complete a voice call with {$recipientName} ({$this->voiceCallLog->getFormattedDuration()}).";
                $data["action"] = Action::STATUS_FAILURE;
            },
        ];

        if (array_key_exists($this->voiceCallLog->getStatus(), $statusHandlers)) {
            $statusHandlers[$this->voiceCallLog->getStatus()]();
        }

        return $data;
    }
}