SessionInitializer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 0
f 0
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A initialize() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Service;
6
7
use AbterPhp\Admin\Domain\Entities\User;
8
use AbterPhp\Framework\Constant\Session;
9
use Opulence\Sessions\ISession;
10
11
class SessionInitializer
12
{
13
    protected ISession $session;
14
15
    /**
16
     * SessionInitializer constructor.
17
     *
18
     * @param ISession $session
19
     */
20
    public function __construct(ISession $session)
21
    {
22
        $this->session = $session;
23
    }
24
25
    /**
26
     * @param User $user
27
     */
28
    public function initialize(User $user)
29
    {
30
        if ($this->session->has(Session::USER_ID) && $this->session->get(Session::USER_ID) === $user->getId()) {
31
            return;
32
        }
33
34
        $this->session->set(Session::IS_LOGGED_IN, true);
35
        $this->session->set(Session::USER_ID, $user->getId());
36
        $this->session->set(Session::USERNAME, $user->getUsername());
37
        $this->session->set(Session::EMAIL, $user->getEmail());
38
        $this->session->set(Session::IS_GRAVATAR_ALLOWED, $user->isGravatarAllowed());
39
        $this->session->set(Session::LANGUAGE_IDENTIFIER, $user->getUserLanguage()->getIdentifier());
40
    }
41
}
42