Completed
Push — 2.x ( aeb5ce...f5bd0f )
by Jindřich
03:11
created

UserLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
3
namespace SkautisBundle\Security\Authentication;
4
5
6
use SkautisBundle\Security\Authentication\Connector\SkautisUserConnectorInterface;
7
use SkautisBundle\Security\Authentication\Registrator\UserRegistratorInterface;
8
9
class UserLoader
10
{
11
    /**
12
     * @var bool
13
     */
14
    private $enableConnector ;
15
    /**
16
     * @var SkautisUserConnectorInterface
17
     */
18
    private $userConnector;
19
20
    /**
21
     * @var bool
22
     */
23
    private $enableAutoRegister;
24
25
    /**
26
     * @var UserRegistratorInterface
27
     */
28
    private $userRegistrator;
29
30
    /**
31
     * UserLoader constructor.
32
     * @param bool $enableConnector
33
     * @param SkautisUserConnectorInterface $userConnector
34
     * @param bool $enableAutoRegister
35
     * @param UserRegistratorInterface $userRegistrator
36
     */
37
    public function __construct($enableConnector = false, SkautisUserConnectorInterface $userConnector = null, $enableAutoRegister = false, UserRegistratorInterface $userRegistrator = null)
38
    {
39
        $this->enableConnector = $enableConnector;
40
        $this->userConnector = $userConnector;
41
        $this->enableAutoRegister = $enableAutoRegister;
42
        $this->userRegistrator = $userRegistrator;
43
    }
44
45
46
    public function loadUser($personId, $userProvider)
47
    {
48
        if (!$this->enableConnector) {
49
            return null;
50
        }
51
52
        if (!$this->userConnector) {
53
            throw new \Exception("No userConnector set while autoregistration enabled"); //@TODO custom exception
54
        }
55
56
        $user = null;
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
        $username = $this->userConnector->getUsername($personId);
58
        if (!empty($username)) {
59
            return $userProvider->loadUserByUsername($username);
60
        }
61
62
63
        if (!$this->enableAutoRegister) {
64
            return null;
65
        }
66
67
        if (!$this->userRegistrator) {
68
            throw new \Exception("No registrator set while autoregistration enabled"); //@TODO custom exception
69
        }
70
71
        $username = $this->userRegistrator->registerUser();  //@TODO log registration
72
        $this->userConnector->connect($personId, $username);
73
74
        return $userProvider->loadUserByUsername($username);
75
    }
76
}