Skip to content

Instantly share code, notes, and snippets.

@jakzal
Last active August 29, 2015 14:00
Show Gist options
  • Save jakzal/3772afcec2e6674b2f54 to your computer and use it in GitHub Desktop.
Save jakzal/3772afcec2e6674b2f54 to your computer and use it in GitHub Desktop.
Controller as a listener
<?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();
}
}
<?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);
}
}
<?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;
}
}
<?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