Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

src/Kunstmaan/FormBundle/Helper/FormHandler.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\FormBundle\Helper;
4
5
use ArrayObject;
6
use Doctrine\ORM\EntityManager;
7
use Kunstmaan\FormBundle\Entity\FormAdaptorInterface;
8
use Kunstmaan\FormBundle\Entity\FormSubmission;
9
use Kunstmaan\FormBundle\Entity\FormSubmissionField;
10
use Kunstmaan\FormBundle\Event\FormEvents;
11
use Kunstmaan\FormBundle\Event\SubmissionEvent;
12
use Kunstmaan\NodeBundle\Helper\RenderContext;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Symfony\Component\Form\Extension\Core\Type\FormType;
15
use Symfony\Component\Form\FormBuilderInterface;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Routing\RouterInterface;
19
20
/**
21
 * The form handler handles everything from creating the form to handling the submitted form
22
 */
23
class FormHandler implements FormHandlerInterface
24
{
25
    /**
26
     * @var ContainerInterface
27
     */
28
    private $container;
29
30
    /**
31
     * @param ContainerInterface $container
32
     */
33
    public function __construct(ContainerInterface $container)
34
    {
35
        $this->container = $container;
36
    }
37
38
    /**
39
     * @param FormPageInterface $page    The form page
40
     * @param Request           $request The request
41
     * @param RenderContext     $context The render context
42
     *
43
     * @return RedirectResponse|void|null
44
     */
45
    public function handleForm(FormPageInterface $page, Request $request, RenderContext $context)
46
    {
47
        /* @var $em EntityManager */
48
        $em = $this->container->get('doctrine.orm.entity_manager');
49
        /* @var $formBuilder FormBuilderInterface */
50
        $formBuilder = $this->container->get('form.factory')->createBuilder(FormType::class);
51
        /* @var $router RouterInterface */
52
        $router = $this->container->get('router');
53
        /* @var $fields ArrayObject */
54
        $fields = new ArrayObject();
55
        $pageParts = $em->getRepository('KunstmaanPagePartBundle:PagePartRef')->getPageParts($page, $page->getFormElementsContext());
56
        foreach ($pageParts as $sequence => $pagePart) {
57
            if ($pagePart instanceof FormAdaptorInterface) {
58
                $pagePart->adaptForm($formBuilder, $fields, $sequence);
59
            }
60
        }
61
62
        $form = $formBuilder->getForm();
63
        if ($request->getMethod() == 'POST') {
64
            $form->handleRequest($request);
65
            if ($form->isSubmitted() && $form->isValid()) {
66
                $formSubmission = new FormSubmission();
67
                $formSubmission->setIpAddress($request->getClientIp());
68
                $formSubmission->setNode($em->getRepository('KunstmaanNodeBundle:Node')->getNodeFor($page));
69
                $formSubmission->setLang($request->getLocale());
70
                $em->persist($formSubmission);
71
72
                /* @var $field FormSubmissionField */
73
                foreach ($fields as $field) {
74
                    $field->setSubmission($formSubmission);
75
                    $field->onValidPost($form, $formBuilder, $request, $this->container);
76
                    $em->persist($field);
77
                }
78
79
                $em->flush();
80
                $em->refresh($formSubmission);
81
82
                $event = new SubmissionEvent($formSubmission, $page);
0 ignored issues
show
$page is of type object<Kunstmaan\FormBun...lper\FormPageInterface>, but the function expects a object<Kunstmaan\NodeBundle\Entity\AbstractPage>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
                $this->container->get('event_dispatcher')->dispatch(FormEvents::ADD_SUBMISSION, $event);
84
85
                $from = $page->getFromEmail();
86
                $to = $page->getToEmail();
87
                $subject = $page->getSubject();
88
                if (!empty($from) && !empty($to) && !empty($subject)) {
89
                    $mailer = $this->container->get('kunstmaan_form.form_mailer');
90
                    $mailer->sendContactMail($formSubmission, $from, $to, $subject);
91
                }
92
93
                return new RedirectResponse($page->generateThankYouUrl($router, $context));
94
            }
95
        }
96
        $context["frontendform"] = $form->createView();
97
        $context["frontendformobject"] = $form;
98
99
        return null;
100
    }
101
}
102