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

AbstractNewsletterController::getFormTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
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