Completed
Push — 5.3 ( 958546...1cc96e )
by Jeroen
14:02 queued 07:05
created

Controller/FormSubmissionsController.php (1 issue)

call_checks.maybe_mismatching_type_passed_with_def

Bug Minor

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\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\AdminBundle\Helper\Security\Acl\Permission\PermissionMap;
7
use Kunstmaan\AdminListBundle\AdminList\AdminList;
8
use Kunstmaan\AdminListBundle\AdminList\ExportList;
9
use Kunstmaan\FormBundle\AdminList\FormPageAdminListConfigurator;
10
use Kunstmaan\FormBundle\AdminList\FormSubmissionAdminListConfigurator;
11
use Kunstmaan\FormBundle\AdminList\FormSubmissionExportListConfigurator;
12
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
13
use Symfony\Component\Routing\Annotation\Route;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
15
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Kunstmaan\AdminBundle\FlashMessages\FlashTypes;
20
21
/**
22
 * The controller which will handle everything related with form pages and form submissions
23
 */
24
class FormSubmissionsController extends Controller
25
{
26
    /**
27
     * The index action will use an admin list to list all the form pages
28
     *
29
     * @Route("/", name="KunstmaanFormBundle_formsubmissions")
30
     * @Template("KunstmaanAdminListBundle:Default:list.html.twig")
31
     *
32
     * @return array
33
     */
34
    public function indexAction(Request $request)
35
    {
36
        /* @var EntityManager $em */
37
        $em = $this->getDoctrine()->getManager();
38
        $aclHelper = $this->container->get('kunstmaan_admin.acl.helper');
39
40
        /* @var AdminList $adminList */
41
        $adminList = $this->get('kunstmaan_adminlist.factory')->createList(
42
            new FormPageAdminListConfigurator($em, $aclHelper, PermissionMap::PERMISSION_VIEW),
43
            $em
44
        );
45
        $adminList->bindRequest($request);
46
47
        return array('adminlist' => $adminList);
48
    }
49
50
    /**
51
     * The list action will use an admin list to list all the form submissions related to the given $nodeTranslationId
52
     *
53
     * @param int $nodeTranslationId
54
     *
55
     * @Route("/list/{nodeTranslationId}", requirements={"nodeTranslationId" = "\d+"},
56
     *                                     name="KunstmaanFormBundle_formsubmissions_list", methods={"GET", "POST"})
57
     * @Template("@KunstmaanForm/FormSubmissions/list.html.twig")
58
     *
59
     * @return array
60
     */
61
    public function listAction(Request $request, $nodeTranslationId)
62
    {
63
        $em = $this->getDoctrine()->getManager();
64
        $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($nodeTranslationId);
65
66
        /** @var AdminList $adminList */
67
        $adminList = $this->get('kunstmaan_adminlist.factory')->createList(
68
            new FormSubmissionAdminListConfigurator($em, $nodeTranslation, $this->getParameter('kunstmaan_form.deletable_formsubmissions')),
69
            $em
70
        );
71
        $adminList->bindRequest($request);
72
73
        return array('nodetranslation' => $nodeTranslation, 'adminlist' => $adminList);
74
    }
75
76
    /**
77
     * The edit action will be used to edit a given submission.
78
     *
79
     * @param int $nodeTranslationId The node translation id
80
     * @param int $submissionId      The submission id
81
     *
82
     * @Route("/list/{nodeTranslationId}/{submissionId}", requirements={"nodeTranslationId" = "\d+", "submissionId" =
83
     *                                                    "\d+"}, name="KunstmaanFormBundle_formsubmissions_list_edit", methods={"GET", "POST"})
84
     * @Template("@KunstmaanForm/FormSubmissions/edit.html.twig")
85
     *
86
     * @return array
87
     */
88
    public function editAction($nodeTranslationId, $submissionId)
89
    {
90
        $em = $this->getDoctrine()->getManager();
91
        $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($nodeTranslationId);
92
        $formSubmission = $em->getRepository('KunstmaanFormBundle:FormSubmission')->find($submissionId);
93
        $request = $this->container->get('request_stack')->getCurrentRequest();
94
        $deletableFormsubmission = $this->getParameter('kunstmaan_form.deletable_formsubmissions');
95
96
        /** @var AdminList $adminList */
97
        $adminList = $this->get('kunstmaan_adminlist.factory')->createList(
98
            new FormSubmissionAdminListConfigurator($em, $nodeTranslation, $deletableFormsubmission),
99
            $em
100
        );
101
        $adminList->bindRequest($request);
102
103
        return [
104
            'nodetranslation' => $nodeTranslation,
105
            'formsubmission' => $formSubmission,
106
            'adminlist' => $adminList,
107
        ];
108
    }
109
110
    /**
111
     * Export as CSV of all the form submissions for the given $nodeTranslationId
112
     *
113
     * @param int $nodeTranslationId
114
     *
115
     * @Route("/export/{nodeTranslationId}.{_format}", requirements={"nodeTranslationId" = "\d+","_format" =
116
     *                                                 "csv|xlsx|ods"}, name="KunstmaanFormBundle_formsubmissions_export", methods={"GET"})
117
     *
118
     * @return Response
119
     */
120
    public function exportAction($nodeTranslationId, $_format)
121
    {
122
        $em = $this->getDoctrine()->getManager();
123
        /** @var NodeTranslation $nodeTranslation */
124
        $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($nodeTranslationId);
125
        $translator = $this->get('translator');
126
127
        /** @var ExportList $exportList */
128
        $configurator = new FormSubmissionExportListConfigurator($em, $nodeTranslation, $translator);
129
        $exportList = $this->get('kunstmaan_adminlist.factory')->createExportList($configurator);
130
131
        return $this->get('kunstmaan_adminlist.service.export')->getDownloadableResponse($exportList, $_format);
132
    }
133
134
    /**
135
     * @Route(
136
     *      "/{id}/delete",
137
     *      requirements={"id" = "\d+"},
138
     *      name="KunstmaanFormBundle_formsubmissions_delete",
139
     *      methods={"POST"}
140
     * )
141
     *
142
     * @param Request $request
143
     * @param int     $id
144
     *
145
     * @return RedirectResponse
146
     *
147
     * @throws AccessDeniedException
148
     */
149
    public function deleteAction(Request $request, $id)
150
    {
151
        $em = $this->getDoctrine()->getManager();
152
        $submission = $em->getRepository('KunstmaanFormBundle:FormSubmission')->find($id);
153
154
        $node = $em->getRepository('KunstmaanNodeBundle:Node')->find($submission->getNode());
155
        $nt = $node->getNodeTranslation($request->getLocale());
156
157
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_DELETE, $node);
158
159
        $url = $this->get('router')->generate(
160
            'KunstmaanFormBundle_formsubmissions_list',
161
            ['nodeTranslationId' => $nt->getId()]
162
        );
163
164
        $fields = $em->getRepository('KunstmaanFormBundle:FormSubmissionField')->findBy(['formSubmission' => $submission]);
165
166
        try {
167
            foreach ($fields as $field) {
168
                $em->remove($field);
169
            }
170
171
            $em->remove($submission);
0 ignored issues
show
It seems like $submission defined by $em->getRepository('Kuns...Submission')->find($id) on line 152 can also be of type null; however, Doctrine\Common\Persiste...ObjectManager::remove() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
172
            $em->flush();
173
174
            $this->addFlash(
175
                FlashTypes::SUCCESS,
176
                $this->get('translator')->trans('formsubmissions.delete.flash.success')
177
            );
178
        } catch (\Exception $e) {
179
            $this->get('logger')->error($e->getMessage());
180
            $this->addFlash(
181
                FlashTypes::ERROR,
182
                $this->get('translator')->trans('formsubmissions.delete.flash.error')
183
            );
184
        }
185
186
        $response = new RedirectResponse($url);
187
188
        return $response;
189
    }
190
}
191