Conditions | 16 |
Paths | 26 |
Total Lines | 51 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
120 | public function answerAction(Request $request, Event $event) |
||
121 | { |
||
122 | if (!$this->canGiveFeedback($event)) { |
||
123 | return $this->json(false); |
||
124 | } |
||
125 | |||
126 | //get request fields |
||
127 | $identifier = $request->request->get('identifier'); |
||
128 | $questionNumber = $request->request->get('questionNumber'); |
||
129 | $value = $request->request->get('value'); |
||
130 | $private = $request->request->get('private') === 'true'; |
||
131 | $action = $request->request->get('action', 'override'); |
||
132 | |||
133 | //ensure all fields set |
||
134 | if (!isset($identifier) || !isset($questionNumber) || !isset($value) || !isset($private) || !\in_array($action, ['override', 'ensure_value_exists', 'remove_value'], true)) { |
||
135 | return $this->json(false); |
||
136 | } |
||
137 | |||
138 | //get participant or create a new one |
||
139 | $participant = $this->getDoctrine()->getRepository(Participant::class)->findOneBy(['identifier' => $identifier, 'event' => $event->getId()]); |
||
140 | if ($participant === null) { |
||
141 | $participant = new Participant(); |
||
142 | $participant->setEvent($event); |
||
143 | $participant->setIdentifier($identifier); |
||
144 | $this->fastSave($participant); |
||
145 | } |
||
146 | |||
147 | //try to find existing answer |
||
148 | $conditions = ['questionNumber' => $questionNumber, 'participant' => $participant->getId()]; |
||
149 | if ($action === 'ensure_value_exists' || $action === 'remove_value') { |
||
150 | $conditions += ['value' => $value]; |
||
151 | } |
||
152 | $answer = $this->getDoctrine()->getRepository(Answer::class)->findOneBy($conditions); |
||
153 | |||
154 | //create new of not found && not want to remove anyways |
||
155 | if ($answer === null && $action !== 'remove_value') { |
||
156 | $answer = new Answer(); |
||
157 | $answer->setParticipant($participant); |
||
158 | $answer->setPrivate($private); |
||
159 | $answer->setQuestionNumber($questionNumber); |
||
160 | } |
||
161 | $answer->setValue($value); |
||
162 | |||
163 | //process actions |
||
164 | if ($action === 'override' || $action === 'ensure_value_exists') { |
||
165 | $this->fastSave($answer); |
||
166 | } elseif ($action === 'remove_value' && $answer !== null) { |
||
167 | $this->fastRemove($answer); |
||
168 | } |
||
169 | |||
170 | return $this->json(true); |
||
171 | } |
||
218 |