Passed
Push — master ( acea2e...2d21bb )
by Anton
29s
created

UpdateAction::run()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
/**
3
 * @author Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\jsonapi\actions;
7
8
use yii\base\Model;
9
use yii\db\ActiveRecord;
10
use Yii;
11
use yii\web\ServerErrorHttpException;
12
13
class UpdateAction extends Action
14
{
15
    /**
16
     * @var string the scenario to be assigned to the model before it is validated and updated.
17
     */
18
    public $scenario = Model::SCENARIO_DEFAULT;
19
20
    /**
21
     * Updates an existing resource.
22
     * @param string $id the primary key of the model.
23
     * @return \yii\db\ActiveRecordInterface the model being updated
24
     * @throws ServerErrorHttpException if there is any error when updating the model
25
     */
26
    public function run($id)
27
    {
28
        /* @var $model ActiveRecord */
29
        $model = $this->findModel($id);
30
31
        if ($this->checkAccess) {
32
            call_user_func($this->checkAccess, $this->id, $model);
33
        }
34
35
        $request = Yii::$app->getRequest();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
36
        $model->scenario = $this->scenario;
37
        $model->load($request->getBodyParams());
38
        if ($model->save() === false && !$model->hasErrors()) {
39
            throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
40
        }
41
42
        $this->linkRelationships($model, $request->getBodyParam('relationships'));
43
44
        return $model;
45
    }
46
}