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

UsersAssociatedTrait::saveUsersAssociatedModels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 13
ccs 10
cts 11
cp 0.9091
crap 2.003
rs 9.9332
c 0
b 0
f 0
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