Test Failed
Pull Request — master (#22)
by Anton
04:03
created

UpdateAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 34
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 20 4
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
}