InboundVoiceCallCommand.php 2.51 KB
<?php

namespace NexmoBundle\Event\Logger\Command;

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 InboundVoiceCallCommand extends VoiceCallAuditLogCommand
{
    public function isProcessable()
    {
        return parent::isProcessable();
    }

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

        // actor will be admin recipient, since activity logs should always show in admin user's context
        if ($actor) {
            $data["actor"] = $this->voiceCallLog->getRecipient();
        }

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

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

        return $data;
    }
}