AuthTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 71
ccs 0
cts 39
cp 0
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUserDataById() 0 12 2
A loginUsers() 0 16 2
A loginSocial() 0 20 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Traits;
6
7
use Canvas\Models\Users;
8
use Baka\Auth\Models\Sessions;
0 ignored issues
show
Bug introduced by
The type Baka\Auth\Models\Sessions was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Canvas\Exception\NotFoundException;
10
11
/**
12
 * Trait ResponseTrait.
13
 *
14
 * @package Canvas\Traits
15
 *
16
 * @property Users $user
17
 * @property Config $config
18
 * @property Request $request
19
 * @property Auth $auth
20
 * @property \Phalcon\Di $di
21
 *
22
 */
23
trait AuthTrait
24
{
25
    /**
26
     * Login user.
27
     * @param string
28
     * @return array
29
     */
30
    private function loginUsers(string $email, string $password): array
31
    {
32
        $userIp = !defined('API_TESTS') ? $this->request->getClientAddress() : '127.0.0.1';
33
34
        $userData = Users::login($email, $password, 1, 0, $userIp);
35
        $token = $userData->getToken();
36
37
        //start session
38
        $session = new Sessions();
39
        $session->start($userData, $token['sessionId'], $token['token'], $userIp, 1);
40
41
        return [
42
            'token' => $token['token'],
43
            'time' => date('Y-m-d H:i:s'),
44
            'expires' => date('Y-m-d H:i:s', time() + $this->config->jwt->payload->exp),
45
            'id' => $userData->getId(),
46
        ];
47
    }
48
49
    /**
50
     * User Login Social.
51
     * @param string
52
     * @return array
53
     */
54
    private function loginSocial(Users $user): array
55
    {
56
        $userIp = !defined('API_TESTS') ? $this->request->getClientAddress() : '127.0.0.1';
57
58
        $user->lastvisit = date('Y-m-d H:i:s');
59
        $user->user_login_tries = 0;
60
        $user->user_last_login_try = 0;
61
        $user->update();
62
63
        $token = $user->getToken();
64
65
        //start session
66
        $session = new Sessions();
67
        $session->start($user, $token['sessionId'], $token['token'], $userIp, 1);
68
69
        return [
70
            'token' => $token['token'],
71
            'time' => date('Y-m-d H:i:s'),
72
            'expires' => date('Y-m-d H:i:s', time() + $this->config->jwt->payload->exp),
73
            'id' => $user->getId(),
74
        ];
75
    }
76
77
    /**
78
     * Set user into Di by id.
79
     * @param int $usersId
80
     * @return void
81
     */
82
    private function setUserDataById(int $usersId): void
83
    {
84
        $hostUser = Users::findFirstOrFail([
85
            'conditions' => 'id = ?0 and status = 1 and is_deleted = 0',
86
            'bind' => [$usersId]
87
        ]);
88
89
        /**
90
         * Set the host in di.
91
         */
92
        if (!$this->di->has('userData')) {
93
            $this->di->setShared('userData', $hostUser);
94
        }
95
    }
96
}
97