Passed
Push — master ( c2b947...93db86 )
by Carlos
02:53
created

Create::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3.0123

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 0
dl 0
loc 25
ccs 16
cts 18
cp 0.8889
crap 3.0123
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace roaresearch\yii2\roa\actions;
4
5
use roaresearch\yii2\roa\hal\ARContract;
6
use Yii;
7
use yii\{base\Model, web\ServerErrorHttpException};
8
9
/**
10
 * Action to create a record.
11
 * @author Angel (Faryshta) Guevara <[email protected]>
12
 */
13
class Create extends Action
14
{
15
    use LoadFileTrait;
16
17
    /**
18
     * @var string the scenario to be assigned to the new model before it is validated and saved.
19
     */
20
    public string $scenario = Model::SCENARIO_DEFAULT;
21
22
    /**
23
     * Creates a new model.
24
     * @return ARContract the model newly created
25
     * @throws ServerErrorHttpException if there is any error when creating the model
26
     */
27 2
    public function run(): ARContract
28
    {
29 2
        $request = Yii::$app->getRequest();
30
        /* @var $model ARContract */
31 2
        $model = new ($this->modelClass)([
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected '(' on line 31 at column 21
Loading history...
32 2
            'scenario' => $this->scenario,
33
        ]);
34 2
        $model->load(
35 2
            $request->getQueryParams() + $request->getBodyParams(),
36
            ''
37
        );
38 2
        $this->checkAccess($model, $request->getQueryParams());
39 2
        $model->load($this->parseFileAttributes(), '');
40 2
        if ($model->save()) {
41 2
            $response = Yii::$app->getResponse();
42 2
            $response->setStatusCode(201);
43 2
            $response->getHeaders()->set('Location', $model->getSelfLink());
44 2
        } elseif (!$model->hasErrors()) {
45
            throw new ServerErrorHttpException(
46
                'Failed to create the object for unknown reason.'
47
            );
48
        }
49
50 2
        return $model;
51
    }
52
}
53