VoiceCallAuditLogCommand.php 1.33 KB
<?php

namespace NexmoBundle\Event\Logger;

use AppBundle\Entity\User;
use AppBundle\Entity\VoiceCall;
use AppBundle\Event\VoiceCallEvent;
use Symfony\Component\EventDispatcher\Event;

abstract class VoiceCallAuditLogCommand
{
    /**
     * @var VoiceCallEvent
     */
    protected $event;

    /**
     * @var VoiceCall
     */
    protected $voiceCallLog;

    protected $logData = [];

    public function isProcessable()
    {
        $this->event = $this->event instanceof VoiceCallEvent ? $this->event : null;
        $this->voiceCallLog = $this->event ? $this->event->getVoiceCall() : null;

        return $this->event && $this->voiceCallLog instanceof VoiceCall && $this->voiceCallLog->getDuration() > 0;
    }

    abstract protected function buildLogData();

    protected function commonLogData()
    {
        /**
         * actor, currently our webhook sets caller as the employee and recipient as the employee
         */
        return array(
            "actionDateTime" => new \DateTime(),
            "actor" => $this->event->getVoiceCall()->getRecipient()
        );
    }

    final public function resolve(Event $event)
    {
        $this->event = $event;
        if ($this->isProcessable()) {
            $this->logData = array_merge($this->commonLogData(), $this->buildLogData()) ;
        }

        return $this->logData;
    }
}