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