Completed
Push — master ( a7fff6...5ea170 )
by Jeroen
18:57 queued 10s
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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Symfony\Component\Routing\Annotation\Route;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
12
use Symfony\Component\HttpFoundation\Request;
13
14
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...
15
{
16
    /**
17
     * @Route("/{popup}", name="popup_newsletter_index", requirements={"popup": "\d+"})
18
     */
19
    public function indexAction($popup)
20
    {
21
        /** @var \Kunstmaan\LeadGenerationBundle\Entity\Popup\AbstractPopup $thePopup */
22
        $thePopup = $this->getDoctrine()->getRepository('KunstmaanLeadGenerationBundle:Popup\AbstractPopup')->find($popup);
23
        $form = $this->createSubscriptionForm($thePopup);
24
25
        return $this->render($this->getIndexTemplate(), array(
26
            'popup' => $thePopup,
27
            'form' => $form->createView(),
28
        ));
29
    }
30
31
    /**
32
     * @Route("/{popup}/subscribe", name="popup_newsletter_subscribe", requirements={"popup": "\d+"}, methods={"POST"})
33
     * @Template()
34
     */
35
    public function subscribeAction(Request $request, $popup)
36
    {
37
        /** @var \Kunstmaan\LeadGenerationBundle\Entity\Popup\AbstractPopup $thePopup */
38
        $thePopup = $this->getDoctrine()->getRepository('KunstmaanLeadGenerationBundle:Popup\AbstractPopup')->find($popup);
39
        $form = $this->createSubscriptionForm($thePopup);
40
41
        $form->handleRequest($request);
42
        if ($form->isSubmitted() && $form->isValid()) {
43
            $this->handleSubscription($request, $form->getData(), $thePopup);
44
45
            return $this->render($this->getThanksTemplate(), array(
46
                'popup' => $thePopup,
47
            ));
48
        }
49
50
        return $this->render($this->getFormTemplate(), array(
51
            'popup' => $thePopup,
52
            'form' => $form->createView(),
53
        ));
54
    }
55
56
    /**
57
     * @param AbstractPopup $popup
58
     *
59
     * @return \Symfony\Component\Form\Form
60
     */
61
    protected function createSubscriptionForm(AbstractPopup $popup)
62
    {
63
        $form = $this->createForm($this->getSubscriptionFormType(), null, array(
64
            'method' => 'POST',
65
            'action' => $this->generateUrl('popup_newsletter_subscribe', array('popup' => $popup->getId())),
66
        ));
67
        $form->add('submit', SubmitType::class, array(
68
            'attr' => array('class' => $popup->getHtmlId() . '--submit'),
69
        ));
70
71
        return $form;
72
    }
73
74
    protected function getSubscriptionFormType()
75
    {
76
        return NewsletterSubscriptionType::class;
77
    }
78
79
    protected function getIndexTemplate()
80
    {
81
        return 'KunstmaanLeadGenerationBundle:Newsletter:index.html.twig';
82
    }
83
84
    protected function getFormTemplate()
85
    {
86
        return 'KunstmaanLeadGenerationBundle:Newsletter:form.html.twig';
87
    }
88
89
    protected function getThanksTemplate()
90
    {
91
        return 'KunstmaanLeadGenerationBundle:Newsletter:thanks.html.twig';
92
    }
93
94
    /**
95
     * @param Request       $request
96
     * @param array         $data
97
     * @param AbstractPopup $popup
98
     */
99
    protected function handleSubscription(Request $request, $data, AbstractPopup $popup)
100
    {
101
        // Implement you own logic here
102
    }
103
}
104