| Conditions | 18 |
| Paths | 102 |
| Total Lines | 62 |
| Code Lines | 36 |
| 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 | $payload = json_decode($request->getContent()); |
||
| 106 | $requiredFields = ['identifier', 'questionIndex', 'value', 'action']; |
||
| 107 | foreach ($requiredFields as $requiredField) { |
||
| 108 | if (!property_exists($payload, $requiredField)) { |
||
| 109 | return $this->json(false); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | //write fields |
||
| 114 | $identifier = $payload->identifier; |
||
| 115 | $questionIndex = $payload->questionIndex; |
||
| 116 | $value = $payload->value; |
||
| 117 | $action = $payload->action; |
||
| 118 | $private = property_exists($payload, 'private') && $payload->private === 'true'; |
||
| 119 | |||
| 120 | //ensure all fields set & valid |
||
| 121 | if (!isset($identifier) || !\is_int($questionIndex) || !isset($value) || !\in_array($action, ['override', 'ensure_value_exists', 'remove_value'], true)) { |
||
| 122 | dump($request->getContent()); |
||
| 123 | |||
| 124 | return $this->json(3); |
||
| 125 | } |
||
| 126 | |||
| 127 | //get participant or create a new one |
||
| 128 | $participant = $this->getDoctrine()->getRepository(Participant::class)->findOneBy(['identifier' => $identifier, 'event' => $event->getId()]); |
||
| 129 | if ($participant === null) { |
||
| 130 | $participant = new Participant(); |
||
| 131 | $participant->setEvent($event); |
||
| 132 | $participant->setIdentifier($identifier); |
||
| 133 | $this->fastSave($participant); |
||
| 134 | } |
||
| 135 | |||
| 136 | //try to find existing answer |
||
| 137 | $conditions = ['questionIndex' => $questionIndex, 'participant' => $participant->getId()]; |
||
| 138 | if ($action === 'ensure_value_exists' || $action === 'remove_value') { |
||
| 139 | $conditions += ['value' => $value]; |
||
| 140 | } |
||
| 141 | $answer = $this->getDoctrine()->getRepository(Answer::class)->findOneBy($conditions); |
||
| 142 | |||
| 143 | //create new of not found && not want to remove anyways |
||
| 144 | if ($answer === null && $action !== 'remove_value') { |
||
| 145 | $answer = new Answer(); |
||
| 146 | $answer->setParticipant($participant); |
||
| 147 | $answer->setPrivate($private); |
||
| 148 | $answer->setQuestionIndex($questionIndex); |
||
| 149 | } |
||
| 150 | $answer->setValue($value); |
||
| 151 | |||
| 152 | //process actions |
||
| 153 | if ($action === 'override' || $action === 'ensure_value_exists') { |
||
| 154 | $this->fastSave($answer); |
||
| 155 | } elseif ($action === 'remove_value' && $answer !== null) { |
||
| 156 | $this->fastRemove($answer); |
||
| 157 | } |
||
| 158 | |||
| 159 | return $this->json(true); |
||
| 160 | } |
||
| 240 |