| Conditions | 5 |
| Paths | 6 |
| Total Lines | 26 |
| Code Lines | 13 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 28 | public function actionCreate() |
||
| 29 | { |
||
| 30 | $username = \Yii::$app->request->post('username'); |
||
| 31 | |||
| 32 | $model = Account::findOne(['username' => $username]); |
||
| 33 | |||
| 34 | if ($model === null) { |
||
| 35 | $model = new Account(); |
||
| 36 | $model->setScenario($this->createScenario); |
||
| 37 | } |
||
| 38 | $isNewRecord = $model->isNewRecord; |
||
| 39 | |||
| 40 | $model->load(\Yii::$app->getRequest()->getBodyParams(), ''); |
||
| 41 | if ($model->save()) { |
||
| 42 | $response = \Yii::$app->getResponse(); |
||
| 43 | $response->setStatusCode($isNewRecord ? 201 : 200); |
||
| 44 | |||
| 45 | // TODO uncomment after create "view" endpoint |
||
| 46 | // $id = implode(',', array_values($model->getPrimaryKey(true))); |
||
| 47 | // $response->getHeaders()->set('Location', Url::toRoute(['view', 'id' => $id], true)); |
||
| 48 | |||
| 49 | } elseif (!$model->hasErrors()) { |
||
| 50 | throw new ServerErrorHttpException('Failed to create the object for unknown reason.'); |
||
| 51 | } |
||
| 52 | |||
| 53 | return $model; |
||
| 54 | } |
||
| 55 | } |