Completed
Pull Request — master (#2)
by Angel
03:03
created

Update   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 38
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A run() 0 17 3
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 update the attributes in a record.
10
 * @author Angel (Faryshta) Guevara <[email protected]>
11
 */
12
class Update extends Action
13
{
14
    use LoadFileTrait;
15
16
    /**
17
     * @var string the scenario to be assigned to the model before it is validated and updated.
18
     */
19
    public $scenario = Model::SCENARIO_DEFAULT;
20
21
    /**
22
     * @var string[] that defines which columns will be recibe files
23
     */
24
    public $fileAttributes = [];
25
26
    /**
27
     * Updates an existing model.
28
     * @param mixed $id the primary key of the model.
29
     * @return \yii\db\ActiveRecordInterface the model being updated
30
     * @throws ServerErrorHttpException if there is any error when updating the model
31
     */
32
    public function run($id)
33
    {
34
        /* @var $model \yii\db\ActiveRecordInterface */
35
        $model = $this->findModel($id);
36
        $request = Yii::$app->getRequest();
37
        $this->checkAccess($model, $request->getQueryParams());
38
        $model->scenario = $this->scenario;
39
        $model->load(
40
            $request->getBodyParams() + $this->parseFileAttributes(),
41
            ''
42
        );
43
        if ($model->save() === false && !$model->hasErrors()) {
44
            throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
45
        }
46
47
        return $model;
48
    }
49
}
50