Completed
Push — master ( 6db6da...5370d9 )
by Angel
03:42
created

ProfileUpdate   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 3 1
A run() 0 14 3
1
<?php
2
3
namespace roaresearch\yii2\roa\actions;
4
5
use Yii;
6
use yii\{base\Model, web\ServerErrorHttpException};
7
8
class ProfileUpdate extends \yii\rest\Action
9
{
10
    /**
11
     * @var string the scenario to be assigned to the model before it is validated and updated.
12
     */
13
    public $scenario = Model::SCENARIO_DEFAULT;
14
15
    /**
16
     * @inheritdoc
17
     */
18
    public function init()
19
    {
20
    }
21
22
    /**
23
     * Updates the information of the logged user.
24
     *
25
     * @return \yii\db\ActiveRecordInterface
26
     * @throws ServerErrorHttpException if there is any error when updating the model
27
     */
28
    public function run()
29
    {
30
        /* @var $model \yii\db\ActiveRecordInterface */
31
        $model = Yii::$app->user->identity;
32
        $model->scenario = $this->scenario;
33
        $model->load(Yii::$app->getRequest()->getBodyParams(), '');
34
        if ($model->save() === false && !$model->hasErrors()) {
35
            throw new ServerErrorHttpException(
36
                'Failed to update the object for unknown reason.'
37
            );
38
        }
39
40
        return $model;
41
    }
42
}
43