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

UsersAssociatedTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 25
ccs 11
cts 12
cp 0.9167
rs 10
c 0
b 0
f 0
wmc 2

1 Method

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