<?php
namespace Fifa\ConnectId\Sdk\Registration\Event\Handler;

use Fifa\ConnectId\Sdk\Model\PersonDetailsRequest;
use Fifa\ConnectId\Sdk\Registration\PersonDetailsProviderInterface;
use Fifa\ConnectId\Sdk\Util\Data\Message\ActionType;
use Fifa\ConnectId\Sdk\Util\Data\Message\MessageProperties;
use Fifa\ConnectId\Sdk\Util\SdkVersionHelper;
use Fifa\ConnectServiceBus\Sdk\FifaConnectServiceBusClientInterface;
use Fifa\ConnectServiceBus\Sdk\Message\Message;

/**
 * Class DefaultPersonDetailsRequestReceivedHandler
 * @package Fifa\ConnectId\Sdk\Registration\Event\Handler
 */
class DefaultPersonDetailsRequestReceivedHandler implements EventHandlerInterface
{
    /**
     * @var PersonDetailsProviderInterface
     */
    private $personDetailsProvider;

    /**
     * @var FifaConnectServiceBusClientInterface
     */
    private $client;

    /**
     * @var string
     */
    private $sdkVersion;

    /**
     * Constructor
     *
     * @param PersonDetailsProviderInterface $personDetailsProvider
     * @param FifaConnectServiceBusClientInterface $client
     */
    public function __construct(PersonDetailsProviderInterface $personDetailsProvider, FifaConnectServiceBusClientInterface $client)
    {
        $this->personDetailsProvider = $personDetailsProvider;
        $this->client = $client;
        $this->sdkVersion = SdkVersionHelper::getSdkVersion();
    }

    /**
     * Code that will be executed when Event will be raised
     *
     * @param PersonDetailsRequest $personDetailsRequest
     */
    public function execute($personDetailsRequest)
    {
        $newMessage = new Message(
            $this->personDetailsProvider->getPersonDetails($personDetailsRequest),
            ActionType::PERSON_DETAILS,
            array(
                MessageProperties::CORRELATION_ID => $personDetailsRequest->getCorrelationId(),
                MessageProperties::VERSION => $this->sdkVersion
            )
        );

        $this->client->send($personDetailsRequest->getReplyTo(), $newMessage);
    }
}