| 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 |
||
| 98 | public function answerAction(Request $request, Event $event) |
||
| 99 | { |
||
| 100 | if (!$this->canGiveFeedback($event)) { |
||
| 101 | return $this->json(false); |
||
| 102 | } |
||
| 103 | |||
| 104 | //get request fields |
||
| 105 | $identifier = $request->request->get('identifier'); |
||
| 106 | $questionNumber = $request->request->get('questionNumber'); |
||
| 107 | $value = $request->request->get('value'); |
||
| 108 | $private = $request->request->get('private') === 'true'; |
||
| 109 | $action = $request->request->get('action', 'override'); |
||
| 110 | |||
| 111 | //ensure all fields set |
||
| 112 | if (!isset($identifier) || !isset($questionNumber) || !isset($value) || !isset($private) || !\in_array($action, ['override', 'ensure_value_exists', 'remove_value'], true)) { |
||
| 113 | return $this->json(false); |
||
| 114 | } |
||
| 115 | |||
| 116 | //get participant or create a new one |
||
| 117 | $participant = $this->getDoctrine()->getRepository(Participant::class)->findOneBy(['identifier' => $identifier, 'event' => $event->getId()]); |
||
| 118 | if ($participant === null) { |
||
| 119 | $participant = new Participant(); |
||
| 120 | $participant->setEvent($event); |
||
| 121 | $participant->setIdentifier($identifier); |
||
| 122 | $this->fastSave($participant); |
||
| 123 | } |
||
| 124 | |||
| 125 | //try to find existing answer |
||
| 126 | $conditions = ['questionNumber' => $questionNumber, 'participant' => $participant->getId()]; |
||
| 127 | if ($action === 'ensure_value_exists' || $action === 'remove_value') { |
||
| 128 | $conditions += ['value' => $value]; |
||
| 129 | } |
||
| 130 | $answer = $this->getDoctrine()->getRepository(Answer::class)->findOneBy($conditions); |
||
| 131 | |||
| 132 | //create new of not found && not want to remove anyways |
||
| 133 | if ($answer === null && $action !== 'remove_value') { |
||
| 134 | $answer = new Answer(); |
||
| 135 | $answer->setParticipant($participant); |
||
| 136 | $answer->setPrivate($private); |
||
| 137 | $answer->setQuestionNumber($questionNumber); |
||
| 138 | } |
||
| 139 | $answer->setValue($value); |
||
| 140 | |||
| 141 | //process actions |
||
| 142 | if ($action === 'override' || $action === 'ensure_value_exists') { |
||
| 143 | $this->fastSave($answer); |
||
| 144 | } elseif ($action === 'remove_value' && $answer !== null) { |
||
| 145 | $this->fastRemove($answer); |
||
| 146 | } |
||
| 147 | |||
| 148 | return $this->json(true); |
||
| 149 | } |
||
| 229 |