-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchAction.php
78 lines (68 loc) · 2.18 KB
/
SearchAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace AppBundle\Action;
use Sidus\FilterBundle\Registry\QueryHandlerRegistry;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Routing\RouterInterface;
/**
* Basic search action
*/
class SearchAction
{
/** @var EngineInterface */
protected $renderEngine;
/** @var QueryHandlerRegistry */
protected $queryHandlerRegistry;
/** @var FormFactoryInterface */
protected $formFactory;
/** @var RouterInterface */
protected $router;
/**
* @param EngineInterface $renderEngine
* @param QueryHandlerRegistry $queryHandlerRegistry
* @param FormFactoryInterface $formFactory
* @param RouterInterface $router
*/
public function __construct(
EngineInterface $renderEngine,
QueryHandlerRegistry $queryHandlerRegistry,
FormFactoryInterface $formFactory,
RouterInterface $router
) {
$this->renderEngine = $renderEngine;
$this->queryHandlerRegistry = $queryHandlerRegistry;
$this->formFactory = $formFactory;
$this->router = $router;
}
/**
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function __invoke(Request $request)
{
// Create form with filters
$builder = $this->formFactory->createBuilder(
FormType::class,
null,
[
'method' => 'get',
'csrf_protection' => false,
'action' => $this->router->generate(self::class),
'validation_groups' => ['filters'],
]
);
$queryHandler = $this->queryHandlerRegistry->getQueryHandler('news');
$form = $queryHandler->buildForm($builder);
$queryHandler->handleRequest($request);
return $this->renderEngine->renderResponse(
'Search/action.html.twig',
[
'form' => $form->createView(),
'results' => $queryHandler->getPager(),
]
);
}
}