CreateAction   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 15
c 1
b 1
f 0
dl 0
loc 35
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 22 4
1
<?php
2
3
namespace app\core\actions;
4
5
use Yii;
6
use yii\base\InvalidConfigException;
7
use yii\base\Model;
8
use yii\db\ActiveRecordInterface;
9
use yii\rest\Action;
10
use yii\web\ServerErrorHttpException;
11
12
class CreateAction extends Action
13
{
14
    /**
15
     * @var string the scenario to be assigned to the new model before it is validated and saved.
16
     */
17
    public $scenario = Model::SCENARIO_DEFAULT;
18
19
20
    /**
21
     * Creates a new model.
22
     * @return ActiveRecordInterface the model newly created
23
     * @throws ServerErrorHttpException|InvalidConfigException if there is any error when creating the model
24
     */
25
    public function run()
26
    {
27
        if ($this->checkAccess) {
28
            call_user_func($this->checkAccess, $this->id);
29
        }
30
31
        /* @var $model \yii\db\ActiveRecord */
32
        $model = new $this->modelClass([
33
            'scenario' => $this->scenario,
34
        ]);
35
36
        $model->load(Yii::$app->getRequest()->getBodyParams(), '');
37
        if ($model->save()) {
38
            $response = Yii::$app->getResponse();
39
            $response->setStatusCode(200);
40
            $id = implode(',', array_values($model->getPrimaryKey(true)));
41
            return $model->findOne($id);
42
        } elseif (!$model->hasErrors()) {
43
            throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
44
        }
45
46
        return $model;
47
    }
48
}
49