UserLoader::loadUser()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 29
rs 8.439
cc 6
eloc 15
nc 6
nop 2
1
<?php
2
3
namespace SkautisBundle\Security\Authentication;
4
5
6
use SkautisBundle\Exception\ConfigurationException;
7
use SkautisBundle\Security\Authentication\Connector\UserConnectorInterface;
8
use SkautisBundle\Security\Authentication\Registrator\UserRegistratorInterface;
9
10
class UserLoader
11
{
12
    /**
13
     * @var bool
14
     */
15
    private $enableConnector;
16
    /**
17
     * @var UserConnectorInterface
18
     */
19
    private $userConnector;
20
21
    /**
22
     * @var bool
23
     */
24
    private $enableAutoRegister;
25
26
    /**
27
     * @var UserRegistratorInterface
28
     */
29
    private $userRegistrator;
30
31
    /**
32
     * UserLoader constructor.
33
     * @param bool $enableConnector
34
     * @param UserConnectorInterface $userConnector
35
     * @param bool $enableAutoRegister
36
     * @param UserRegistratorInterface $userRegistrator
37
     */
38
    public function __construct($enableConnector = false, UserConnectorInterface $userConnector = null, $enableAutoRegister = false, UserRegistratorInterface $userRegistrator = null)
39
    {
40
        $this->enableConnector = $enableConnector;
41
        $this->userConnector = $userConnector;
42
        $this->enableAutoRegister = $enableAutoRegister;
43
        $this->userRegistrator = $userRegistrator;
44
    }
45
46
47
    public function loadUser($personId, $userProvider)
48
    {
49
        if (!$this->enableConnector) {
50
            return null;
51
        }
52
53
        if (!$this->userConnector) {
54
            throw new ConfigurationException("No userConnector set while autoregistration enabled");
55
        }
56
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 ConfigurationException("No registrator set while autoregistration enabled");
69
        }
70
71
        $username = $this->userRegistrator->registerUser();
72
        $this->userConnector->connect($personId, $username);
73
74
        return $userProvider->loadUserByUsername($username);
75
    }
76
}