| Conditions | 12 |
| Paths | 268 |
| Total Lines | 99 |
| Code Lines | 62 |
| 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 |
||
| 107 | public function run(RequestInterface $request, ResponseInterface $response) |
||
| 108 | { |
||
| 109 | try { |
||
| 110 | $failMessage = $this->translator()->translation('Failed to create object'); |
||
| 111 | $errorThrown = strtr($this->translator()->translation('{{ errorMessage }}: {{ errorThrown }}'), [ |
||
| 112 | '{{ errorMessage }}' => $failMessage |
||
| 113 | ]); |
||
| 114 | $reqMessage = $this->translator()->translation( |
||
| 115 | '{{ parameter }} required, must be a {{ expectedType }}, received {{ actualType }}' |
||
| 116 | ); |
||
| 117 | |||
| 118 | $objType = $request->getParam('obj_type'); |
||
| 119 | if (!$objType) { |
||
| 120 | $actualType = is_object($objType) ? get_class($objType) : gettype($objType); |
||
| 121 | $this->addFeedback('error', strtr($reqMessage, [ |
||
| 122 | '{{ parameter }}' => '"obj_type"', |
||
| 123 | '{{ expectedType }}' => 'string', |
||
| 124 | '{{ actualType }}' => $actualType, |
||
| 125 | ])); |
||
| 126 | $this->setSuccess(false); |
||
| 127 | |||
| 128 | return $response->withStatus(400); |
||
| 129 | } |
||
| 130 | |||
| 131 | // Create or load object (From `ObjectContainerTrait`) |
||
| 132 | $obj = $this->obj(); |
||
| 133 | $obj->setFlatData($this->saveData()); |
||
| 134 | |||
| 135 | $valid = $obj->validate(); |
||
| 136 | if (!$valid) { |
||
| 137 | if (!$this->hasFeedbacks()) { |
||
| 138 | $this->addFeedback('error', strtr($errorThrown, [ |
||
| 139 | '{{ errorThrown }}' => $this->translator()->translation('Invalid Data') |
||
| 140 | ])); |
||
| 141 | } |
||
| 142 | |||
| 143 | $this->addFeedbackFromValidation($obj); |
||
| 144 | $this->setSuccess(false); |
||
| 145 | |||
| 146 | return $response->withStatus(400); |
||
| 147 | } |
||
| 148 | |||
| 149 | if ($obj instanceof ContentInterface) { |
||
| 150 | $authorIdent = $this->authorIdent(); |
||
| 151 | if (!$obj->lastModifiedBy()) { |
||
| 152 | $obj->setLastModifiedBy($authorIdent); |
||
| 153 | } |
||
| 154 | |||
| 155 | if (!$obj->createdBy()) { |
||
| 156 | $obj->setCreatedBy($authorIdent); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | $result = $obj->save(); |
||
| 161 | |||
| 162 | if ($result) { |
||
| 163 | $this->setObj($obj); |
||
| 164 | |||
| 165 | $this->addFeedback('success', $this->translator()->translate('Object has been successfully created.')); |
||
| 166 | $this->addFeedback('success', strtr($this->translator()->translate('Created Object: {{ objId }}'), [ |
||
| 167 | '{{ objId }}' => $obj->id() |
||
| 168 | ])); |
||
| 169 | $this->addFeedbackFromValidation($obj, [ ModelValidator::NOTICE, ModelValidator::WARNING ]); |
||
| 170 | $this->setSuccess(true); |
||
| 171 | |||
| 172 | return $response; |
||
| 173 | } else { |
||
| 174 | $this->setObj(null); |
||
| 175 | |||
| 176 | $this->addFeedback('error', $failMessage); |
||
| 177 | $this->addFeedbackFromValidation($obj); |
||
| 178 | $this->setSuccess(false); |
||
| 179 | |||
| 180 | return $response->withStatus(500); |
||
| 181 | } |
||
| 182 | } catch (PDOException $e) { |
||
| 183 | $this->setObj(null); |
||
| 184 | |||
| 185 | if (isset($e->errorInfo[2])) { |
||
| 186 | $message = $e->errorInfo[2]; |
||
| 187 | } else { |
||
| 188 | $message = $e->getMessage(); |
||
| 189 | } |
||
| 190 | |||
| 191 | $this->addFeedback('error', strtr($errorThrown, [ |
||
| 192 | '{{ errorThrown }}' => $message |
||
| 193 | ])); |
||
| 194 | $this->setSuccess(false); |
||
| 195 | |||
| 196 | return $response->withStatus(500); |
||
| 197 | } catch (Exception $e) { |
||
| 198 | $this->setObj(null); |
||
| 199 | |||
| 200 | $this->addFeedback('error', strtr($errorThrown, [ |
||
| 201 | '{{ errorThrown }}' => $e->getMessage() |
||
| 202 | ])); |
||
| 203 | $this->setSuccess(false); |
||
| 204 | |||
| 205 | return $response->withStatus(500); |
||
| 206 | } |
||
| 209 |