Completed
Push — master ( 6db6da...5370d9 )
by Angel
03:42
created

Create::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace roaresearch\yii2\roa\actions;
4
5
use Yii;
6
use yii\{base\Model, web\ServerErrorHttpException};
7
8
/**
9
 * Action to create a record.
10
 * @author Angel (Faryshta) Guevara <[email protected]>
11
 */
12
class Create extends Action
13
{
14
    use LoadFileTrait;
15
16
    /**
17
     * @var string the scenario to be assigned to the new model before it is validated and saved.
18
     */
19
    public $scenario = Model::SCENARIO_DEFAULT;
20
21
    /**
22
     * Creates a new model.
23
     * @return \yii\db\ActiveRecordInterface the model newly created
24
     * @throws ServerErrorHttpException if there is any error when creating the model
25
     */
26
    public function run()
27
    {
28
        $request = Yii::$app->getRequest();
29
        $modelClass = $this->modelClass;
30
        /* @var $model \yii\db\ActiveRecordInterface */
31
        $model = new $modelClass([
32
            'scenario' => $this->scenario,
33
        ]);
34
        $model->load(
35
            $request->getQueryParams() + $request->getBodyParams(),
36
            ''
37
        );
38
        $this->checkAccess($model, $request->getQueryParams());
39
        $model->load($this->parseFileAttributes(), '');
40
        if ($model->save()) {
41
            $response = Yii::$app->getResponse();
42
            $response->setStatusCode(201);
43
            $response->getHeaders()->set('Location', $model->getSelfLink());
0 ignored issues
show
Bug introduced by
The method getSelfLink() does not seem to exist on object<yii\db\ActiveRecordInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
        } elseif (!$model->hasErrors()) {
45
            throw new ServerErrorHttpException(
46
                'Failed to create the object for unknown reason.'
47
            );
48
        }
49
50
        return $model;
51
    }
52
}
53