44
55use FrequenceWeb \Bundle \ContactBundle \EventDispatcher \ContactEvents ;
66use FrequenceWeb \Bundle \ContactBundle \EventDispatcher \Event \MessageSubmitEvent ;
7- use Symfony \Component \DependencyInjection \ContainerAwareInterface ;
8- use Symfony \Component \DependencyInjection \ContainerAwareTrait ;
7+ use FrequenceWeb \Bundle \ContactBundle \Model \Contact ;
8+ use Psr \Container \ContainerInterface ;
9+ use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
10+ use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
911use Symfony \Component \HttpFoundation \RedirectResponse ;
1012use Symfony \Component \HttpFoundation \Request ;
1113use Symfony \Component \Form \FormInterface ;
14+ use Symfony \Component \HttpFoundation \Response ;
15+ use Symfony \Component \HttpFoundation \Session \SessionInterface ;
16+ use Symfony \Contracts \Service \ServiceSubscriberInterface ;
17+ use Symfony \Contracts \Translation \TranslatorInterface ;
1218
1319/**
1420 * Contact controller
1521 *
1622 * @author Yohan Giarelli <yohan@giarel.li>
1723 */
18- class DefaultController implements ContainerAwareInterface
24+ class DefaultController extends AbstractController
1925{
20- use ContainerAwareTrait;
21-
2226 /**
2327 * Action that displays the contact form
2428 *
2529 * @param Request $request
2630 *
27- * @return \Symfony\Component\HttpFoundation\ Response
31+ * @return Response
2832 */
29- public function indexAction (Request $ request )
33+ public function indexAction (Request $ request ): Response
3034 {
3135 $ this ->container ->get ('session ' )->set ('_fw_contact_referer ' , $ request ->getUri ());
3236
@@ -38,17 +42,19 @@ public function indexAction(Request $request)
3842 *
3943 * @param Request $request
4044 *
41- * @return \Symfony\Component\HttpFoundation\ Response
45+ * @return Response
4246 */
43- public function submitAction (Request $ request )
47+ public function submitAction (Request $ request ): Response
4448 {
4549 $ form = $ this ->getForm ();
4650 $ form ->handleRequest ($ request );
4751
48- if ($ form ->isValid ()) {
52+ if ($ form ->isSubmitted () && $ form -> isValid ()) {
4953 // Send the event for message handling (send mail, add to DB, don't care)
5054 $ event = new MessageSubmitEvent ($ form ->getData ());
51- $ this ->container ->get ('event_dispatcher ' )->dispatch (ContactEvents::onMessageSubmit, $ event );
55+ /** @var EventDispatcherInterface $eventDispatcher */
56+ $ eventDispatcher = $ this ->container ->get ('event_dispatcher ' );
57+ $ eventDispatcher ->dispatch ($ event , ContactEvents::onMessageSubmit);
5258
5359 // Let say the user it's ok
5460 $ message = $ this ->container ->get ('translator ' )->trans ('contact.submit.success ' , [], 'FrequenceWebContactBundle ' );
@@ -71,12 +77,12 @@ public function submitAction(Request $request)
7177 *
7278 * @param FormInterface $form
7379 *
74- * @return \Symfony\Component\HttpFoundation\ Response
80+ * @return Response
7581 */
7682 protected function renderFormResponse (FormInterface $ form )
7783 {
78- return $ this ->container -> get ( ' templating ' )-> renderResponse (
79- 'FrequenceWebContactBundle: Default: index.html.twig ' ,
84+ return $ this ->render (
85+ '@ FrequenceWebContactBundle/ Default/ index.html.twig ' ,
8086 ['form ' => $ form ->createView ()]
8187 );
8288 }
@@ -86,11 +92,24 @@ protected function renderFormResponse(FormInterface $form)
8692 *
8793 * @return FormInterface
8894 */
89- protected function getForm ()
95+ protected function getForm (): FormInterface
9096 {
9197 return $ this ->container ->get ('form.factory ' )->create (
9298 $ this ->container ->getParameter ('frequence_web_contact.type ' ),
9399 $ this ->container ->get ('frequence_web_contact.model ' )
94100 );
95101 }
102+
103+ /**
104+ * @inheritDoc
105+ */
106+ public static function getSubscribedServices ()
107+ {
108+ $ dependentServices = parent ::getSubscribedServices ();
109+
110+ return array_merge ($ dependentServices , [
111+ 'event_dispatcher ' => EventDispatcherInterface::class,
112+ 'translator ' => TranslatorInterface::class,
113+ ]);
114+ }
96115}
0 commit comments