Passed
Push — master ( 1b1f16...a29a7e )
by Mihail
03:34
created

ActionUpdate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B update() 0 24 5
1
<?php
2
3
namespace Apps\Controller\Admin\User;
4
5
use Apps\Model\Admin\User\FormUserUpdate;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\View;
8
use Ffcms\Core\Exception\SyntaxException;
9
use Ffcms\Core\Helper\Type\Any;
10
use Ffcms\Core\Network\Request;
11
use Ffcms\Core\Network\Response;
12
use Apps\ActiveRecord\User as UserRecord;
13
14
/**
15
 * Trait ActionUpdate
16
 * @package Apps\Controller\Admin\User
17
 * @property Request $request
18
 * @property Response $response
19
 * @property View $view
20
 */
21
trait ActionUpdate
22
{
23
    /**
24
     * Edit user profile by id
25
     * @param string $id
26
     * @return string
27
     * @throws SyntaxException
28
     */
29
    public function update(string $id): ?string
30
    {
31
        if (!Any::isInt($id) || $id < 1) {
32
            throw new SyntaxException('Wrong id syntax');
33
        }
34
        $user = UserRecord::findOrNew($id);
35
        // generate model data based on user object
36
        $model = new FormUserUpdate($user);
37
38
        // check is form is sended
39
        if ($model->send()) {
40
            if ($model->validate()) { // check validation
41
                $model->save();
42
                App::$Session->getFlashBag()->add('success', __('Data was successful updated'));
43
            } else {
44
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
45
            }
46
        }
47
48
        // render viewer
49
        return $this->view->render('user_update', [
50
            'model' => $model
51
        ]);
52
    }
53
}
54