Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

Controller/AbstractNewsletterController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\LeadGenerationBundle\Controller;
4
5
use Kunstmaan\LeadGenerationBundle\Entity\Popup\AbstractPopup;
6
use Kunstmaan\LeadGenerationBundle\Form\NewsletterSubscriptionType;
7
use Symfony\Component\Routing\Annotation\Route;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
11
use Symfony\Component\HttpFoundation\Request;
12
13
abstract class AbstractNewsletterController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
14
{
15
    /**
16
     * @Route("/{popup}", name="popup_newsletter_index", requirements={"popup": "\d+"})
17
     */
18
    public function indexAction($popup)
19
    {
20
        /** @var AbstractPopup $thePopup */
21
        $thePopup = $this->getDoctrine()->getRepository(AbstractPopup::class)->find($popup);
22
        $form = $this->createSubscriptionForm($thePopup);
23
24
        return $this->render($this->getIndexTemplate(), array(
25
            'popup' => $thePopup,
26
            'form' => $form->createView(),
27
        ));
28
    }
29
30
    /**
31
     * @Route("/{popup}/subscribe", name="popup_newsletter_subscribe", requirements={"popup": "\d+"}, methods={"POST"})
32
     * @Template()
33
     */
34
    public function subscribeAction(Request $request, $popup)
35
    {
36
        /** @var AbstractPopup $thePopup */
37
        $thePopup = $this->getDoctrine()->getRepository(AbstractPopup::class)->find($popup);
38
        $form = $this->createSubscriptionForm($thePopup);
39
40
        $form->handleRequest($request);
41
        if ($form->isSubmitted() && $form->isValid()) {
42
            $this->handleSubscription($request, $form->getData(), $thePopup);
43
44
            return $this->render($this->getThanksTemplate(), array(
45
                'popup' => $thePopup,
46
            ));
47
        }
48
49
        return $this->render($this->getFormTemplate(), array(
50
            'popup' => $thePopup,
51
            'form' => $form->createView(),
52
        ));
53
    }
54
55
    /**
56
     * @param AbstractPopup $popup
57
     *
58
     * @return \Symfony\Component\Form\Form
59
     */
60
    protected function createSubscriptionForm(AbstractPopup $popup)
61
    {
62
        $form = $this->createForm($this->getSubscriptionFormType(), null, array(
63
            'method' => 'POST',
64
            'action' => $this->generateUrl('popup_newsletter_subscribe', array('popup' => $popup->getId())),
65
        ));
66
        $form->add('submit', SubmitType::class, array(
67
            'attr' => array('class' => $popup->getHtmlId() . '--submit'),
68
        ));
69
70
        return $form;
71
    }
72
73
    protected function getSubscriptionFormType()
74
    {
75
        return NewsletterSubscriptionType::class;
76
    }
77
78
    protected function getIndexTemplate()
79
    {
80
        return '@KunstmaanLeadGeneration/Newsletter/index.html.twig';
81
    }
82
83
    protected function getFormTemplate()
84
    {
85
        return '@KunstmaanLeadGeneration/Newsletter/form.html.twig';
86
    }
87
88
    protected function getThanksTemplate()
89
    {
90
        return '@KunstmaanLeadGeneration/Newsletter/thanks.html.twig';
91
    }
92
93
    /**
94
     * @param Request       $request
95
     * @param array         $data
96
     * @param AbstractPopup $popup
97
     */
98
    protected function handleSubscription(Request $request, $data, AbstractPopup $popup)
99
    {
100
        // Implement you own logic here
101
    }
102
}
103