Passed
Pull Request — master (#55)
by Rafael
05:48
created

UsersAssociatedTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 21
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A saveUsersAssociatedModels() 0 13 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Traits;
6
7
use Exception;
8
9
/**
10
 * Trait ResponseTrait
11
 *
12
 * @package Gewaer\Traits
13
 *
14
 * @property Users $user
15
 * @property Config $config
16
 * @property Request $request
17
 * @property Auth $auth
18
 * @property \Phalcon\Di $di
19
 *
20
 */
21
trait UsersAssociatedTrait
22
{
23
    /**
24
     * create new related User Associated instance dynamicly
25
     * @param string $model
26
     * @return void
27
     * @todo Find a better way to handle namespaces for models
28
     */
29 3
    public function saveUsersAssociatedModels(string $model): void
30
    {
31 3
        $class = '\Gewaer\Models\UsersAssociated' . ucfirst($model);
32 3
        $usersAssociatedModel = new $class();
33 3
        $usersAssociatedModel->users_id = $this->user->getId();
34 3
        $usersAssociatedModel->companies_id = $this->getId();
35 3
        $usersAssociatedModel->apps_id = $this->di->getApp()->getId();
36 3
        $usersAssociatedModel->identify_id = $this->user->getId();
37 3
        $usersAssociatedModel->user_active = 1;
38 3
        $usersAssociatedModel->user_role = 'admin';
39
40 3
        if (!$usersAssociatedModel->save()) {
41
            throw new Exception((string)current($usersAssociatedModel->getMessages()));
42
        }
43 3
    }
44
}
45