Passed
Push — master ( d16bab...9efe7c )
by Anton
37s
created

src/actions/UpdateAction.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
}