User::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Faulancer\View\Helper;
4
5
use Faulancer\ORM\Entity;
6
use Faulancer\Service\AuthenticatorService;
7
use Faulancer\Session\SessionManager;
8
use Faulancer\View\AbstractViewHelper;
9
10
/**
11
 * Class Entity
12
 *
13
 * @package Faulancer\View\Helper
14
 * @author  Florian Knapp <[email protected]>
15
 */
16
class User extends AbstractViewHelper
17
{
18
19
    /** @var string */
20
    protected $entity = '';
21
22
    /**
23
     * Initialize
24
     *
25
     * @param string $entity
26
     *
27
     * @return $this
28
     */
29
    public function __invoke(string $entity = '')
30
    {
31
        $this->entity = $entity;
32
        return $this;
33
    }
34
35
    /**
36
     * Check if user is logged in
37
     *
38
     * @return bool
39
     */
40
    public function isLoggedIn()
41
    {
42
        return $this->getServiceLocator()->get(SessionManager::class)->get('user') > 0;
43
    }
44
45
    /**
46
     * Get user entity
47
     *
48
     * @return Entity
49
     */
50
    public function get()
51
    {
52
        /** @var AuthenticatorService $authenticator */
53
        $authenticator = $this->getServiceLocator()->get(AuthenticatorService::class);
54
        return $authenticator->getUserFromSession($this->entity);
55
    }
56
57
    /**
58
     * Check if user has one of given roles
59
     *
60
     * @param array $roles
61
     *
62
     * @return bool
63
     */
64
    public function isPermitted(array $roles)
65
    {
66
        /** @var AuthenticatorService $authenticator */
67
        $authenticator = $this->getServiceLocator()->get(AuthenticatorService::class);
68
69
        return $authenticator->isPermitted($roles);
70
    }
71
72
}