Passed
Pull Request — development (#671)
by Nick
06:47
created

FieldNotesController::deleteMultipleAction()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 4
nop 1
dl 0
loc 33
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\FieldNotes\Controller;
4
5
use AppBundle\Form\DataProvider\UploadFieldNotesDataProvider;
6
use AppBundle\Service\FieldNoteService;
7
use Oc\AbstractController;
8
use AppBundle\Form\UploadFieldNotesType;
9
use AppBundle\Util\DateUtil;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11
use Symfony\Component\HttpFoundation\File\UploadedFile;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
16
/**
17
 * Class FieldNotesController
18
 *
19
 * @package Oc\FieldNotes\Controller
20
 */
21
class FieldNotesController extends AbstractController
22
{
23
    /**
24
     * Index action for field-notes.
25
     *
26
     * @param Request $request
27
     *
28
     * @return Response
29
     *
30
     * @Route("/field-notes/", name="field-notes")
31
     */
32
    public function indexAction(Request $request)
33
    {
34
        $this->denyAccessUnlessGranted('ROLE_USER');
35
        $user = $this->getUser();
36
37
        $fieldNoteService = $this->get(FieldNoteService::class);
38
        $dataProvider = $this->get(UploadFieldNotesDataProvider::class);
39
40
        $repository = $this->getDoctrine()->getRepository('AppBundle:FieldNote');
41
        $fieldNotes = $repository->findBy([
42
            'user' => $user->getId()
43
        ], [
44
            'date' => 'ASC',
45
            'id' => 'ASC'
46
        ]);
47
48
        $form = $this->createForm(UploadFieldNotesType::class, $dataProvider->getData($user->getId()));
49
        $form->handleRequest($request);
50
51
        if ($form->isSubmitted() && $form->isValid()) {
52
            /**
53
             * @var UploadedFile $file
54
             */
55
            $file = $form->getData()[UploadFieldNotesType::FIELD_FILE];
56
57
            try {
58
                $ignoreDate = null;
59
60
                if (!empty($form->getData()[UploadFieldNotesType::FIELD_IGNORE])) {
61
                    $ignoreDate = DateUtil::dateTimeFromMySqlFormat(
62
                        $form->getData()[UploadFieldNotesType::FIELD_IGNORE_DATE]
63
                    );
64
                }
65
66
                $fieldNoteService->importFromFile($file->getRealPath(), $user->getId(), $ignoreDate);
67
            } catch (\Exception $e) {
68
                $this->addErrorMessage($e->getMessage());
69
70
                return $this->redirectToRoute('field-notes');
71
            }
72
73
            if ($fieldNoteService->hasErrors()) {
74
                foreach ($fieldNoteService->getErrors() as $error) {
75
                    $this->addErrorMessage($error);
76
                }
77
78
                return $this->redirectToRoute('field-notes');
79
            }
80
81
            $this->addSuccessMessage(
82
                $this->get('translator')->trans('field_notes.upload.success')
83
            );
84
85
            return $this->redirectToRoute('field-notes');
86
        }
87
88
        $this->setMenu(MNU_MYPROFILE_FIELD_NOTES);
89
        $this->setTitle($this->get('translator')->trans('field_notes.field_notes'));
90
91
        return $this->render('field-notes/index.html.twig', [
92
            'user' => $user,
93
            'form' => $form->createView(),
94
            'fieldNotes' => $fieldNotes,
95
        ]);
96
    }
97
98
    /**
99
     * Action to delete one field-note.
100
     *
101
     * @param int $id
102
     *
103
     * @return RedirectResponse
104
     *
105
     * @Route("/field-notes/delete/{id}", name="field-notes.delete")
106
     */
107
    public function deleteAction($id)
108
    {
109
        $this->denyAccessUnlessGranted('ROLE_USER');
110
        $user = $this->getUser();
111
112
        $repository = $this->getDoctrine()->getRepository('AppBundle:FieldNote');
113
        $fieldNote = $repository->findOneBy([
114
            'user' => $user->getId(),
115
            'id' => $id
116
        ]);
117
118
        if (!$fieldNote) {
119
            return $this->redirectToRoute('field-notes');
120
        }
121
122
        $em = $this->getDoctrine()->getManager();
123
        $em->remove($fieldNote);
124
        $em->flush();
125
126
        $this->addSuccessMessage(
127
            $this->get('translator')->trans('field_notes.success.deleted')
128
        );
129
130
        return $this->redirectToRoute('field-notes');
131
    }
132
133
    /**
134
     * Action to delete multiple field-notes.
135
     *
136
     * @param Request $request
137
     *
138
     * @return RedirectResponse
139
     *
140
     * @Route("/field-notes/delete-multiple/", name="field-notes.delete-multiple")
141
     */
142
    public function deleteMultipleAction(Request $request)
143
    {
144
        $this->denyAccessUnlessGranted('ROLE_USER');
145
        $user = $this->getUser();
146
147
        $selectedFieldNotes = $request->get('selected-field-notes');
148
        if (!is_array($selectedFieldNotes)) {
149
            return $this->redirectToRoute('field-notes');
150
        }
151
152
        $repository = $this->getDoctrine()->getRepository('AppBundle:FieldNote');
153
        $em = $this->getDoctrine()->getManager();
154
155
        foreach ($selectedFieldNotes as $fieldNoteId) {
156
            $fieldNote = $repository->findOneBy([
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $fieldNote is correct as $repository->findOneBy(a... 'id' => $fieldNoteId)) (which targets Doctrine\Common\Persiste...Repository::findOneBy()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
157
                'user' => $user->getId(),
158
                'id' => $fieldNoteId
159
            ]);
160
161
            if (!$fieldNote) {
162
                continue;
163
            }
164
            $em->remove($fieldNote);
165
        }
166
167
        $em->flush();
168
169
        $this->addSuccessMessage(
170
            $this->get('translator')->trans('field_notes.success.deleted_multiple')
171
        );
172
173
        return $this->redirectToRoute('field-notes');
174
    }
175
}
176