Last active
August 29, 2015 14:00
-
-
Save jakzal/3772afcec2e6674b2f54 to your computer and use it in GitHub Desktop.
Controller as a listener
This file contains 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 | |
use Symfony\Component\HttpFoundation\RequestStack; | |
class HttpFacade | |
{ | |
/** | |
* @var RequestStack | |
*/ | |
private $requestStack; | |
public function __construct(RequestStack $requestStack) | |
{ | |
$this->requestStack = $requestStack; | |
} | |
public function setTemplate($template) | |
{ | |
$this->getRequest()->attributes->set('_template', $template); | |
} | |
public function setTemplateVars(array $vars) | |
{ | |
$this->getRequest()->attributes->set('_template_vars', array_keys($vars)); | |
foreach ($vars as $name => $value) { | |
$this->getRequest()->attributes->set($name, $value); | |
} | |
} | |
private function getRequest() | |
{ | |
return $this->requestStack->getCurrentRequest(); | |
} | |
} |
This file contains 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 | |
class SearchController implements SearchListener | |
{ | |
private $searchInteractor; | |
private $httpFacade; | |
private $response; | |
public function __construct(SearchInteractor $searchInteractor, HttpFacade $httpFacade) | |
{ | |
$this->searchInteractor = $searchInteractor; | |
$this->searchInteractor->attach($this); | |
$this->httpFacade = $httpFacade; | |
} | |
/** | |
* @Route("/search", name="search") | |
* @Template | |
*/ | |
public function searchAction(Request $request) | |
{ | |
$this->searchInteractor->search($request->query->get('query')); | |
return $this->response; | |
} | |
public function onSearchResults(array $products) | |
{ | |
$this->httpFacade->setTemplate('AcmeSearchBundle:search:results.html.twig'); | |
$this->httpFacade->setTemplateVars(['products' => $products]); | |
} | |
public function onNoKeywords() | |
{ | |
$this->httpFacade->setTemplate('AcmeSearchBundle:search:noResults.html.twig'); | |
} | |
public function onSearchUnavailable($keywords) | |
{ | |
$this->response = new RedirectResponse('/search-fallback?query='.$keywords); | |
} | |
} |
This file contains 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 | |
class SearchInteractor | |
{ | |
private $repository; | |
private $listener; | |
public function __construct($repository) | |
{ | |
$this->repository = $repository; | |
} | |
public function attach(SearchListener $listener) | |
{ | |
$this->listener = $listener; | |
} | |
public function search($keywords) | |
{ | |
if ($this->isDown()) { | |
$this->getListener()->onSearchUnavailable($keywords); | |
return; | |
} | |
if (empty($keywords)) { | |
$this->getListener()->onNoKeywords(); | |
return; | |
} | |
$products = $this->repository->search($keywords); | |
$this->getListener()->onSearchResults($products); | |
} | |
private function getListener() | |
{ | |
if (null === $this->listener) { | |
throw new \UnexpectedException(); | |
} | |
return $this->listener; | |
} | |
private function isDown() | |
{ | |
// some piece of logic deciding if the service is available | |
return false; | |
} | |
} |
This file contains 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 | |
interface SearchListener | |
{ | |
/** | |
* @param array $products | |
* | |
* @return null | |
*/ | |
public function onSearchResults(array $products); | |
/** | |
* @return null | |
*/ | |
public function onNoKeywords(); | |
/** | |
* @param string $keywords | |
* | |
* @return null | |
*/ | |
public function onSearchUnavailable($keywords); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment