Controller::getAuthenticatedUser()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Abstract controller class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Controllers
7
 * @since      1.0
8
 */
9
namespace EBloodBank\Controllers;
10
11
use EBloodBank\Models\User;
12
use Psr\Container\ContainerInterface;
13
use EBloodBank\Traits\AclTrait;
14
use EBloodBank\Traits\SessionTrait;
15
use EBloodBank\Traits\EventManagerTrait;
16
use EBloodBank\Traits\EntityRepositoryTrait;
17
18
/**
19
 * Abstract controller class
20
 *
21
 * @since 1.0
22
 */
23
abstract class Controller
24
{
25
    use AclTrait;
26
    use SessionTrait;
27
    use EventManagerTrait;
28
    use EntityRepositoryTrait;
29
30
    /**
31
     * @var \Psr\Container\ContainerInterface
32
     * @since 1.2
33
     */
34
    protected $container;
35
36
    /**
37
     * @var \EBloodBank\Views\ViewFactory
38
     * @since 1.6
39
     */
40
    protected $viewFactory;
41
42
    /**
43
     * @var \EBloodBank\Models\User|null
44
     * @since 1.2
45
     */
46
    protected $authenticatedUser;
47
48
    /**
49
     * @since 1.2
50
     */
51
    public function __construct(ContainerInterface $container)
52
    {
53
        $this->container = $container;
54
        $this->setAcl($container->get('acl'));
55
        $this->setSession($container->get('session'));
56
        $this->setEventManager($container->get('eventManager'));
57
        $this->setEntityManager($container->get('entity_manager'));
58
59
        $this->viewFactory = $container->get('viewFactory');
60
        $this->viewFactory->setData('currentUser', $this->getAuthenticatedUser());
61
    }
62
63
    /**
64
     * @var   \Psr\Container\ContainerInterface
65
     * @since 1.2
66
     */
67
    public function getContainer()
68
    {
69
        return $this->container;
70
    }
71
72
    /**
73
     * @return void
74
     * @since  1.6
75
     */
76
    protected function setAuthenticatedUser(User $user)
77
    {
78
        $this->authenticatedUser = $user;
79
    }
80
81
    /**
82
     * @return \EBloodBank\Models\User|null
83
     * @since  1.6
84
     */
85
    protected function findAuthenticatedUser()
86
    {
87
        $segment = $this->getSession()->getSegment('EBloodBank');
88
        $userID = (int) $segment->get('user_id', 0);
89
90
        if (empty($userID)) {
91
            return;
92
        }
93
94
        $user = $this->getUserRepository()->find($userID);
95
96
        return $user;
97
    }
98
99
    /**
100
     * @return \EBloodBank\Models\User|null
101
     * @since  1.6
102
     */
103
    protected function getAuthenticatedUser()
104
    {
105
        if (is_null($this->authenticatedUser)) {
106
            $authenticatedUser = $this->findAuthenticatedUser();
107
            if ($authenticatedUser) {
108
                $this->setAuthenticatedUser($authenticatedUser);
109
            }
110
        }
111
112
        return $this->authenticatedUser;
113
    }
114
115
    /**
116
     * @return bool
117
     * @since  1.6
118
     */
119
    protected function hasAuthenticatedUser()
120
    {
121
        return ($this->getAuthenticatedUser() != null);
122
    }
123
}
124