Failed Conditions
Pull Request — master (#37)
by Rafael
05:30
created

AuthTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 35
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A loginUsers() 0 28 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Traits;
6
7
use Gewaer\Models\Users;
8
use Baka\Auth\Models\Sessions;
9
10
/**
11
 * Trait ResponseTrait
12
 *
13
 * @package Gewaer\Traits
14
 *
15
 * @property Users $user
16
 * @property Config $config
17
 * @property Request $request
18
 * @property Auth $auth
19
 * @property \Phalcon\Di $di
20
 *
21
 */
22
trait AuthTrait
23
{
24
    /**
25
     * Login user
26
     * @param string
27
     * @return array
28
     */
29 2
    private function loginUsers(string $email, string $password): array
30
    {
31 2
        $userIp = !defined('API_TESTS') ? $this->request->getClientAddress() : '127.0.0.1';
32
33 2
        $random = new \Phalcon\Security\Random();
34
35 2
        $userData = Users::login($email, $password, 1, 0, $userIp);
36
37 2
        $sessionId = $random->uuid();
38
39
        //save in user logs
40
        $payload = [
41 2
            'sessionId' => $sessionId,
42 2
            'email' => $userData->getEmail(),
43 2
            'iat' => time(),
44
        ];
45
46 2
        $token = $this->auth->make($payload);
47
48
        //start session
49 2
        $session = new Sessions();
50 2
        $session->start($userData, $sessionId, $token, $userIp, 1);
51
52
        return [
53 2
            'token' => $token,
54 2
            'time' => date('Y-m-d H:i:s'),
55 2
            'expires' => date('Y-m-d H:i:s', time() + $this->config->jwt->payload->exp),
56 2
            'id' => $userData->getId(),
57
        ];
58
    }
59
}
60