Passed
Push — master ( fb1eb0...02cee8 )
by Guido
08:09
created

UserService::isUserLoggedIn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace Gvera\Services;
2
3
use Gvera\Exceptions\NotFoundException;
4
use Gvera\Helpers\entities\GvEntityManager;
5
use Gvera\Helpers\session\Session;
6
use Gvera\Helpers\validation\EmailValidationStrategy;
7
use Gvera\Helpers\validation\ValidationService;
8
use Gvera\Models\User;
9
use Gvera\Models\UserRole;
10
use Gvera\Models\UserRoleAction;
11
use phpDocumentor\Reflection\Types\Boolean;
12
13
/**
14
 * Service Class Doc Comment
15
 *
16
 * @category Class
17
 * @package  src/services
18
 * @author    Guido Vera
19
 * @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
20
 * @link     http://www.github.com/veraguido/gv
21
 *
22
 */
23
class UserService
24
{
25
    const MODERATOR_ROLE_PRIORITY = 5;
26
27
    public GvEntityManager $entityManager;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
28
    public Session $session;
29
    private ValidationService $validationService;
30
31
    public function __construct(GvEntityManager $entityManager, Session $session, ValidationService $validationService)
32
    {
33
        $this->validationService = $validationService;
34
        $this->entityManager = $entityManager;
35
        $this->session = $session;
36
    }
37
38
    /**
39
     * @param $email
40
     * @return bool
41
     * @throws \Exception
42
     */
43
    public function validateEmail($email)
44
    {
45
        return $this->validationService->validate($email, [new EmailValidationStrategy()]);
46
    }
47
48
    /**
49
     * @param $plainPassword
50
     * @return string
51
     */
52
    public function generatePassword($plainPassword)
53
    {
54
        return password_hash($plainPassword, PASSWORD_BCRYPT);
55
    }
56
57
    /**
58
     * @param $plainPassword
59
     * @param $hash
60
     * @return bool
61
     */
62
    public function validatePassword($plainPassword, $hash)
63
    {
64
        return password_verify($plainPassword, $hash);
65
    }
66
67
    /**
68
     * @param $username
69
     * @param $password
70
     * @throws \Exception
71
     */
72
    public function login($username, $password)
73
    {
74
        $repository = $this->entityManager->getRepository(User::class);
75
        $user = $repository->findOneBy(['username' => $username]);
76
77
        if ($user && $user->getUsername() == $username && $this->validatePassword($password, $user->getPassword())) {
78
            $this->session->set(
79
                'user',
80
                [
81
                    'username' => $username,
82
                    'userEmail' => $user->getEmail(),
83
                    'role' => $user->getRole()->getRolePriority()
84
                ]
85
            );
86
        } else {
87
            throw new \Exception('user or password are incorrect');
88
        }
89
    }
90
91
    public function logout()
92
    {
93
        $this->session->unsetByKey('user');
94
    }
95
96
    public function isUserLoggedIn()
97
    {
98
        return $this->session->get('user') != null;
99
    }
100
101
    /**
102
     * @return int
103
     */
104
    public function getUserRole()
105
    {
106
        return $this->session->get('user') != null ? $this->session->get('user')['role'] : false;
107
    }
108
109
110
    /**
111
     * @param User $user
112
     * @param string $userRoleActionName
113
     * @return bool
114
     */
115
    public function userCan(?User $user, string $userRoleActionName):bool
116
    {
117
        if (null === $user) {
118
            return false;
119
        }
120
121
        $action = $this->entityManager->getRepository(UserRoleAction::class)
122
            ->findOneBy(['name' => $userRoleActionName]);
123
124
125
        if (null == $action) {
126
            return false;
127
        }
128
129
        return $user->getRole()->getUserRoleActions()->contains($action);
130
    }
131
}
132