<?php

/**
 * Lifestyle Webconsulting GmbH
 *
 * LICENSE: This Software is the property of Lifestyle Webconsulting GmbH (Aschaffenburg, Germany)
 * and is private by copyright law - it is NOT Freeware.
 *
 * Any unauthorized use of this software without a valid license
 * is a violation of the license agreement and will be prosecuted by
 * civil and criminal law.
 *
 * @copyright  2016 Lifestyle Webconsulting GmbH
 * @link       http://www.life-style.de
 */

namespace Sso\WebserviceBundle\Services\UserApplicationRole\Show;

use Sso\WebserviceBundle\ErrorHandler\ErrorHandlerInterface;
use Sso\WebserviceBundle\Services\HandlerInterface;
use Sso\WebserviceBundle\Services\RequestInterface as ServiceRequestInterface;
use Sso\WebserviceBundle\Services\ResponseBuilderInterface as ServiceResponseBuilderInterface;
use Sso\WebserviceBundle\Database\Webservice\User as UserRepository;
use Sso\WebserviceBundle\Entity\Webservice\Type\User as ModelUser;
use Sso\WebserviceBundle\Services\UserApplicationRole\Show\RequestData\User as RequestDataUser;

/**
 * Class Handler
 *
 * @copyright  2016 Lifestyle Webconsulting GmbH
 * @link       http://www.life-style.de
 * @package Sso\WebserviceBundle\Services\User\Show
 */
final class Handler implements HandlerInterface
{
    /**
     * @var Factory
     */
    private $factory;

    /**
     * @var UserRepository
     */
    private $database;

    /**
     * @var ErrorHandlerInterface
     */
    private $errorHandler;

    /**
     * Handler constructor.
     * @param Factory $factory
     * @param UserRepository $database
     * @param ErrorHandlerInterface $errorHandler
     */
    public function __construct(Factory $factory, UserRepository $database, ErrorHandlerInterface $errorHandler)
    {
        $this->factory = $factory;
        $this->database = $database;
        $this->errorHandler = $errorHandler;
    }

    /**
     * @param ServiceRequestInterface $request
     * @return ServiceResponseBuilderInterface
     */
    public function handle(ServiceRequestInterface $request)
    {


        $requestModel = $this->factory->request()->requestParser()->parse($request->getRequestBody());


        $responseBuilder = $this->factory->response()->responseBuilder();

        if ($this->errorHandler->hasErrors()) {
            return $responseBuilder;
        }

        $userModel = $this->getUserModel($requestModel);

        if (null === $userModel) {
            $this->errorHandler->addError(404, 'UserNotFound', 'u2016', '', '');
            return $responseBuilder;
        }

        //ok next step set the responseDTO
        if($requestModel->getApplicationType()){
            $appName = $requestModel->getApplicationType()->getName();
        } else {
            $appName = false;
        }
        $responseData = $this->factory->responseData()->user()->setFromUserModel($userModel, $appName);

        //set response
        $responseBuilder->setResponse($responseData);

        return $responseBuilder;
    }

    /**
     * @param RequestDataUser $requestModel
     * @return ModelUser|null
     */
    private function getUserModel(RequestDataUser $requestModel)
    {
        $returnData = false;
        if ($identifier = $requestModel->getIdentifier()) {
            $returnData =  $this->database->getUserByIdentifier($identifier);
        }
        if (($guid = $requestModel->getGuid()) && ($returnData === false)) {
            $returnData =  $this->database->getUserByGuid($guid);
        }
        if (($username = $requestModel->getUsername()) && ($returnData === false)) {
            $returnData =  $this->database->getUserByUsername($username);
        }
        if (($email = $requestModel->getEmail()) && ($returnData === false)) {
            $returnData =  $this->database->getUserByEmail($email);
        }
        return $returnData;
    }
}
