Passed
Pull Request — development (#656)
by Nick
06:32
created

FieldNotesController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 155
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 65 7
B deleteAction() 0 25 2
B deleteMultipleAction() 0 33 4
1
<?php
2
3
namespace AppBundle\Controller\FieldNotes;
4
5
use AppBundle\Controller\AbstractController;
6
use AppBundle\Form\UploadFieldNotesType;
7
use AppBundle\Util\DateUtil;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
use Symfony\Component\HttpFoundation\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
14
/**
15
 * Class FieldNotesController
16
 *
17
 * @package AppBundle\Controller\FieldNotes
0 ignored issues
show
introduced by
Unexpected tag type @package in doc block
Loading history...
18
 */
19
class FieldNotesController extends AbstractController
20
{
21
    /**
22
     * Index action for field-notes.
23
     *
24
     * @param Request $request
0 ignored issues
show
introduced by
Request => \Symfony\Component\HttpFoundation\Request
Loading history...
25
     *
26
     * @return Response
0 ignored issues
show
introduced by
Response => \Symfony\Component\HttpFoundation\Response
Loading history...
27
     *
28
     * @Route("/field-notes/", name="field-notes")
29
     */
30
    public function indexAction(Request $request)
31
    {
32
        $this->denyAccessUnlessGranted('ROLE_USER');
33
        $user = $this->getUser();
34
35
        $fieldNoteService = $this->get('app.service.field_note');
36
        $dataProvider = $this->get('app.dataprovider.upload_field_note');
37
38
        $repository = $this->getDoctrine()->getRepository('AppBundle:FieldNote');
39
        $fieldNotes = $repository->findBy([
40
            'user' => $user->getId()
41
        ], [
42
            'date' => 'ASC',
43
            'id' => 'ASC'
44
        ]);
45
46
        $form = $this->createForm(UploadFieldNotesType::class, $dataProvider->getData($user->getId()));
47
        $form->handleRequest($request);
48
49
        if ($form->isSubmitted() && $form->isValid()) {
50
            /**
51
             * @var UploadedFile $file
0 ignored issues
show
introduced by
UploadedFile => \Symfony\Component\HttpFoundation\File\UploadedFile
Loading history...
52
             */
53
            $file = $form->getData()[UploadFieldNotesType::FIELD_FILE];
54
55
            try {
56
                $ignoreDate = null;
57
58
                if (!empty($form->getData()[UploadFieldNotesType::FIELD_IGNORE])) {
59
                    $ignoreDate = DateUtil::dateTimeFromMySqlFormat(
60
                        $form->getData()[UploadFieldNotesType::FIELD_IGNORE_DATE]
61
                    );
62
                }
63
64
                $fieldNoteService->importFromFile($file->getRealPath(), $user->getId(), $ignoreDate);
65
            } catch (\Exception $e) {
66
                $this->addErrorMessage($e->getMessage());
67
68
                return $this->redirectToRoute('field-notes');
69
            }
70
71
            if ($fieldNoteService->hasErrors()) {
72
                foreach ($fieldNoteService->getErrors() as $error) {
73
                    $this->addErrorMessage($error);
74
                }
75
76
                return $this->redirectToRoute('field-notes');
77
            }
78
79
            $this->addSuccessMessage(
80
                $this->get('translator')->trans('field_notes.upload.success')
81
            );
82
83
            return $this->redirectToRoute('field-notes');
84
        }
85
86
        $this->setMenu(MNU_MYPROFILE_FIELD_NOTES);
87
        $this->setTitle($this->get('translator')->trans('field_notes.field_notes'));
88
89
        return $this->render('field-notes/index.html.twig', [
90
            'user' => $user,
91
            'form' => $form->createView(),
92
            'fieldNotes' => $fieldNotes,
93
        ]);
94
    }
95
96
    /**
97
     * Action to delete one field-note.
98
     *
99
     * @param int $id
100
     *
101
     * @return RedirectResponse
0 ignored issues
show
introduced by
RedirectResponse => \Symfony\Component\HttpFoundation\RedirectResponse
Loading history...
102
     *
103
     * @Route("/field-notes/delete/{id}", name="field-notes.delete")
104
     */
105
    public function deleteAction($id)
106
    {
107
        $this->denyAccessUnlessGranted('ROLE_USER');
108
        $user = $this->getUser();
109
110
        $repository = $this->getDoctrine()->getRepository('AppBundle:FieldNote');
111
        $fieldNote = $repository->findOneBy([
112
            'user' => $user->getId(),
113
            'id' => $id
114
        ]);
115
116
        if (!$fieldNote) {
117
            return $this->redirectToRoute('field-notes');
118
        }
119
120
        $em = $this->getDoctrine()->getManager();
121
        $em->remove($fieldNote);
122
        $em->flush();
123
124
        $this->addSuccessMessage(
125
            $this->get('translator')->trans('field_notes.success.deleted')
126
        );
127
128
        return $this->redirectToRoute('field-notes');
129
    }
130
131
    /**
132
     * Action to delete multiple field-notes.
133
     *
134
     * @param Request $request
0 ignored issues
show
introduced by
Request => \Symfony\Component\HttpFoundation\Request
Loading history...
135
     *
136
     * @return RedirectResponse
0 ignored issues
show
introduced by
RedirectResponse => \Symfony\Component\HttpFoundation\RedirectResponse
Loading history...
137
     *
138
     * @Route("/field-notes/delete-multiple/", name="field-notes.delete-multiple")
139
     */
140
    public function deleteMultipleAction(Request $request)
141
    {
142
        $this->denyAccessUnlessGranted('ROLE_USER');
143
        $user = $this->getUser();
144
145
        $selectedFieldNotes = $request->get('selected-field-notes');
146
        if (!is_array($selectedFieldNotes)) {
147
            return $this->redirectToRoute('field-notes');
148
        }
149
150
        $repository = $this->getDoctrine()->getRepository('AppBundle:FieldNote');
151
        $em = $this->getDoctrine()->getManager();
152
153
        foreach ($selectedFieldNotes as $fieldNoteId) {
154
            $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...
155
                'user' => $user->getId(),
156
                'id' => $fieldNoteId
157
            ]);
158
159
            if (!$fieldNote) {
160
                continue;
161
            }
162
            $em->remove($fieldNote);
163
        }
164
165
        $em->flush();
166
167
        $this->addSuccessMessage(
168
            $this->get('translator')->trans('field_notes.success.deleted_multiple')
169
        );
170
171
        return $this->redirectToRoute('field-notes');
172
    }
173
}
174