Passed
Push — master ( 73d14c...8a9029 )
by Mihail
04:02
created

ActionUpdate::profileUpdate()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 5
nop 1
dl 0
loc 27
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Admin\Profile;
4
5
use Apps\ActiveRecord\Profile;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\View;
8
use Ffcms\Core\Exception\NotFoundException;
9
use Ffcms\Core\Helper\Type\Any;
10
use Ffcms\Core\Network\Request;
11
use Ffcms\Core\Network\Response;
12
use Apps\Model\Front\Profile\FormSettings as FrontFormSettings;
13
14
/**
15
 * Trait ActionUpdate
16
 * @package Apps\Controller\Admin\Profile
17
 * @property Request $request
18
 * @property Response $response
19
 * @property View $view
20
 */
21
trait ActionUpdate
22
{
23
    /**
24
     * Edit user profile action
25
     * @param string $id
26
     * @return null|string
27
     * @throws NotFoundException
28
     * @throws \Ffcms\Core\Exception\SyntaxException
29
     */
30
    public function profileUpdate($id): ?string
31
    {
32
        if (!Any::isInt($id) || $id < 1) {
33
            throw new NotFoundException(__('Wrong profile query id'));
34
        }
35
36
        // get user profile via id
37
        $profile = Profile::find($id);
38
        if (!$profile) {
39
            throw new NotFoundException(__('User profile with id %id% not exist', ['id' => $id]));
40
        }
41
42
        // check if user id for this profile_id is exist
43
        if (!App::$User->isExist($profile->user_id)) {
44
            throw new NotFoundException(__('User record not found: %id%', ['id' => $profile->user_id]));
45
        }
46
47
        // initialize settings form and process it
48
        $model = new FrontFormSettings($profile->user);
49
        if ($model->send() && $model->validate()) {
50
            $model->save();
51
            App::$Session->getFlashBag()->add('success', __('Profile is updated'));
52
        }
53
54
        return $this->view->render('profile/update', [
55
            'model' => $model,
56
            'profile' => $profile
57
        ]);
58
    }
59
}