Last active
August 29, 2015 14:01
-
-
Save sasajib/13c85e1f6ca3b39efc22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Sasajib\Pavilion\Api\Video; | |
use Sasajib\Pavilion\Api\Contracts\RequestValidatorInterface; | |
use Sasajib\Pavilion\Api\Exceptions\DatabaseException; | |
use Sasajib\Pavilion\Api\Exceptions\RequestValidationException; | |
use Sasajib\Pavilion\Api\Pagination\Contracts\PaginationFactoryInterface; | |
use Sasajib\Pavilion\Api\Video\Contracts\DatabaseInterface; | |
use Sasajib\Pavilion\Api\Video\Contracts\InteractorInterface; | |
use Sasajib\Pavilion\Api\Video\Contracts\ResponderInterface; | |
class Manager implements InteractorInterface | |
{ | |
/** | |
* @var \Sasajib\Pavilion\Api\Contracts\RequestValidatorInterface | |
*/ | |
protected $validator; | |
/** | |
* @var \Sasajib\Pavilion\Api\Video\Contracts\DatabaseInterface | |
*/ | |
protected $database; | |
/** | |
* @var \Sasajib\Pavilion\Api\Video\Contracts\ResponderInterface | |
*/ | |
private $response; | |
/** | |
* @param ResponderInterface $response | |
*/ | |
public function __construct(ResponderInterface $response) | |
{ | |
$this->response = $response; | |
} | |
/** | |
* @param $limit | |
* @param PaginationFactoryInterface $paginationFactory | |
* @param DatabaseInterface $database | |
* @return mixed | |
*/ | |
public function getHighlightsCollection( | |
$limit, PaginationFactoryInterface $paginationFactory, | |
DatabaseInterface $database | |
) | |
{ | |
try { | |
$this->loadDatabaseForCollection($limit, $paginationFactory, $database); | |
$result = $this->database->getVideoCollection(); | |
return $this->response->showHighlightCollection($result); | |
} catch (RequestValidationException $e) { | |
return $this->response->showValidationError($e); | |
} catch (DatabaseException $e) { | |
return $this->response->showDatabaseError($e); | |
} | |
} | |
/** | |
* @param $id | |
* @param RequestValidatorInterface $validator | |
* @param DatabaseInterface $database | |
* @return mixed | |
*/ | |
public function getHighlight( | |
$id, RequestValidatorInterface $validator, | |
DatabaseInterface $database | |
) | |
{ | |
try { | |
$this->loadSingleRequestDependency($validator, $database); | |
$this->validator->validate($id, null); | |
$result = $this->database->getVideoById($id); | |
return $this->response->showHighlight($result); | |
} catch (RequestValidationException $e) { | |
return $this->response->showValidationError($e); | |
} catch (DatabaseException $e) { | |
return $this->response->showDatabaseError($e); | |
} | |
} | |
/** | |
* @param RequestValidatorInterface $validator | |
*/ | |
public function setValidator(RequestValidatorInterface $validator) | |
{ | |
$this->validator = $validator; | |
} | |
/** | |
* @param DatabaseInterface $database | |
*/ | |
public function setDatabase(DatabaseInterface $database) | |
{ | |
$this->database = $database; | |
} | |
/** | |
* @param RequestValidatorInterface $validator | |
* @param DatabaseInterface $database | |
*/ | |
private function loadSingleRequestDependency( | |
RequestValidatorInterface $validator, DatabaseInterface $database | |
) | |
{ | |
$this->setValidator($validator); | |
$this->setDatabase($database); | |
} | |
/** | |
* @param $limit | |
* @param PaginationFactoryInterface $paginationFactory | |
*/ | |
private function getSetValidatePaginator( | |
$limit, | |
PaginationFactoryInterface $paginationFactory | |
) | |
{ | |
$this->database->setPaginator($paginationFactory->getType($limit)); | |
} | |
/** | |
* @param $limit | |
* @param PaginationFactoryInterface $paginationFactory | |
* @param DatabaseInterface $database | |
*/ | |
private function loadDatabaseForCollection( | |
$limit, PaginationFactoryInterface $paginationFactory, | |
DatabaseInterface $database | |
) | |
{ | |
$this->setDatabase($database); | |
$this->getSetValidatePaginator($limit, $paginationFactory); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment