Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

FormSubmissionsController::listAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 10
cp 0
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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
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...
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("@KunstmaanAdminList/Default/list.html.twig")
31
     *
32
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,AdminList>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
33
     */
34 View Code Duplication
    public function indexAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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)
0 ignored issues
show
Documentation introduced by
$aclHelper is of type object|null, but the function expects a object<Kunstmaan\AdminBu...Security\Acl\AclHelper>.

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...
43
        );
44
        $adminList->bindRequest($request);
45
46
        return array('adminlist' => $adminList);
47
    }
48
49
    /**
50
     * The list action will use an admin list to list all the form submissions related to the given $nodeTranslationId
51
     *
52
     * @param int $nodeTranslationId
53
     *
54
     * @Route("/list/{nodeTranslationId}", requirements={"nodeTranslationId" = "\d+"},
55
     *                                     name="KunstmaanFormBundle_formsubmissions_list", methods={"GET", "POST"})
56
     * @Template("@KunstmaanForm/FormSubmissions/list.html.twig")
57
     *
58
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,object|null>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
59
     */
60
    public function listAction(Request $request, $nodeTranslationId)
61
    {
62
        $em = $this->getDoctrine()->getManager();
63
        $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($nodeTranslationId);
64
65
        /** @var AdminList $adminList */
66
        $adminList = $this->get('kunstmaan_adminlist.factory')->createList(
67
            new FormSubmissionAdminListConfigurator($em, $nodeTranslation, $this->getParameter('kunstmaan_form.deletable_formsubmissions'))
0 ignored issues
show
Documentation introduced by
$nodeTranslation is of type object|null, but the function expects a object<Kunstmaan\NodeBun...Entity\NodeTranslation>.

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...
Compatibility introduced by
$em of type object<Doctrine\Persistence\ObjectManager> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\Persistence\ObjectManager to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
68
        );
69
        $adminList->bindRequest($request);
70
71
        return array('nodetranslation' => $nodeTranslation, 'adminlist' => $adminList);
72
    }
73
74
    /**
75
     * The edit action will be used to edit a given submission.
76
     *
77
     * @param int $nodeTranslationId The node translation id
78
     * @param int $submissionId      The submission id
79
     *
80
     * @Route("/list/{nodeTranslationId}/{submissionId}", requirements={"nodeTranslationId" = "\d+", "submissionId" =
81
     *                                                    "\d+"}, name="KunstmaanFormBundle_formsubmissions_list_edit", methods={"GET", "POST"})
82
     * @Template("@KunstmaanForm/FormSubmissions/edit.html.twig")
83
     *
84
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,object|null>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
85
     */
86
    public function editAction($nodeTranslationId, $submissionId)
87
    {
88
        $em = $this->getDoctrine()->getManager();
89
        $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($nodeTranslationId);
90
        $formSubmission = $em->getRepository('KunstmaanFormBundle:FormSubmission')->find($submissionId);
91
        $request = $this->container->get('request_stack')->getCurrentRequest();
92
        $deletableFormsubmission = $this->getParameter('kunstmaan_form.deletable_formsubmissions');
93
94
        /** @var AdminList $adminList */
95
        $adminList = $this->get('kunstmaan_adminlist.factory')->createList(
96
            new FormSubmissionAdminListConfigurator($em, $nodeTranslation, $deletableFormsubmission)
0 ignored issues
show
Documentation introduced by
$nodeTranslation is of type object|null, but the function expects a object<Kunstmaan\NodeBun...Entity\NodeTranslation>.

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...
Compatibility introduced by
$em of type object<Doctrine\Persistence\ObjectManager> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\Persistence\ObjectManager to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
97
        );
98
        $adminList->bindRequest($request);
99
100
        return [
101
            'nodetranslation' => $nodeTranslation,
102
            'formsubmission' => $formSubmission,
103
            'adminlist' => $adminList,
104
        ];
105
    }
106
107
    /**
108
     * Export as CSV of all the form submissions for the given $nodeTranslationId
109
     *
110
     * @param int $nodeTranslationId
111
     *
112
     * @Route("/export/{nodeTranslationId}.{_format}", requirements={"nodeTranslationId" = "\d+","_format" =
113
     *                                                 "csv|xlsx|ods"}, name="KunstmaanFormBundle_formsubmissions_export", methods={"GET"})
114
     *
115
     * @return Response
116
     */
117
    public function exportAction($nodeTranslationId, $_format)
118
    {
119
        $em = $this->getDoctrine()->getManager();
120
        /** @var NodeTranslation $nodeTranslation */
121
        $nodeTranslation = $em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($nodeTranslationId);
122
        $translator = $this->get('translator');
123
124
        /** @var ExportList $exportList */
125
        $configurator = new FormSubmissionExportListConfigurator($em, $nodeTranslation, $translator);
0 ignored issues
show
Compatibility introduced by
$em of type object<Doctrine\Persistence\ObjectManager> is not a sub-type of object<Doctrine\ORM\EntityManagerInterface>. It seems like you assume a child interface of the interface Doctrine\Persistence\ObjectManager to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
126
        $exportList = $this->get('kunstmaan_adminlist.factory')->createExportList($configurator);
127
128
        return $this->get('kunstmaan_adminlist.service.export')->getDownloadableResponse($exportList, $_format);
129
    }
130
131
    /**
132
     * @Route(
133
     *      "/{id}/delete",
134
     *      requirements={"id" = "\d+"},
135
     *      name="KunstmaanFormBundle_formsubmissions_delete",
136
     *      methods={"POST"}
137
     * )
138
     *
139
     * @param Request $request
140
     * @param int     $id
141
     *
142
     * @return RedirectResponse
143
     *
144
     * @throws AccessDeniedException
145
     */
146
    public function deleteAction(Request $request, $id)
147
    {
148
        $em = $this->getDoctrine()->getManager();
149
        $submission = $em->getRepository('KunstmaanFormBundle:FormSubmission')->find($id);
150
151
        $node = $em->getRepository('KunstmaanNodeBundle:Node')->find($submission->getNode());
152
        $nt = $node->getNodeTranslation($request->getLocale());
153
154
        $this->denyAccessUnlessGranted(PermissionMap::PERMISSION_DELETE, $node);
155
156
        $url = $this->get('router')->generate(
157
            'KunstmaanFormBundle_formsubmissions_list',
158
            ['nodeTranslationId' => $nt->getId()]
159
        );
160
161
        $fields = $em->getRepository('KunstmaanFormBundle:FormSubmissionField')->findBy(['formSubmission' => $submission]);
162
163
        try {
164
            foreach ($fields as $field) {
165
                $em->remove($field);
166
            }
167
168
            $em->remove($submission);
0 ignored issues
show
Bug introduced by
It seems like $submission defined by $em->getRepository('Kuns...Submission')->find($id) on line 149 can also be of type null; however, Doctrine\Persistence\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...
169
            $em->flush();
170
171
            $this->addFlash(
172
                FlashTypes::SUCCESS,
173
                $this->get('translator')->trans('formsubmissions.delete.flash.success')
174
            );
175
        } catch (\Exception $e) {
176
            $this->get('logger')->error($e->getMessage());
177
            $this->addFlash(
178
                FlashTypes::DANGER,
179
                $this->get('translator')->trans('formsubmissions.delete.flash.error')
180
            );
181
        }
182
183
        return new RedirectResponse($url);
184
    }
185
}
186