Completed
Push — develop ( dfd31d...f6e11d )
by
unknown
17:02
created

Auth::factory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Auth\Controller\Plugin;
4
5
use Auth\Entity\User;
6
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
7
use Auth\AuthenticationService as AuthenticationService;
8
use Zend\Mvc\Controller\PluginManager as ControllerManager;
9
10
/**
11
 * @method \Auth\Entity\UserInterface getUser()
12
 */
13
class Auth extends AbstractPlugin
14
{
15
    /**
16
     * @var AuthenticationService
17
     */
18
    protected $authenticationService;
19
20
    /**
21
     * @param AuthenticationService $authenticationService
22
     */
23
    public function __construct(AuthenticationService $authenticationService)
24
    {
25
        $this->authenticationService = $authenticationService;
26
    }
27
    
28
    /**
29
     * @param null $property
30
     *
31
     * @return $this|bool|null
32
     */
33
    public function __invoke($property = null)
34
    {
35
        if (null === $property) {
36
            return $this;
37
        }
38
        if (true === $property) {
39
            return $this->isLoggedIn();
40
        }
41
        return $this->get($property);
42
    }
43
44
    /**
45
     * Checks, if a user is logged in
46
     *
47
     * @return bool
48
     */
49
    public function isLoggedIn()
50
    {
51
        return $this->authenticationService->hasIdentity();
52
    }
53
54
    /**
55
     * Checks, if a user is an Admin
56
     *
57
     * @return bool
58
     */
59
    public function isAdmin()
60
    {
61
        return $this->authenticationService->getUser()->getRole() == User::ROLE_ADMIN;
62
    }
63
64
    /**
65
     * @param $method
66
     * @param $params
67
     *
68
     * @return mixed
69
     */
70
    public function __call($method, $params)
71
    {
72
        $auth = $this->authenticationService;
73
        if (method_exists($auth, $method) && is_callable(array($auth, $method))) {
74
            return call_user_func_array(array($auth, $method), $params);
75
        }
76
        throw new \BadMethodCallException('Unknown method.');
77
    }
78
79
    /**
80
     * @param $property
81
     *
82
     * @return null
83
     */
84
    public function get($property)
85
    {
86
        $auth = $this->authenticationService;
87
        if ($auth->hasIdentity()) {
88
            if (false !== strpos($property, '.')) {
89
                $value = $auth->getUser();
90
                foreach (explode('.', $property) as $prop) {
91
                    $value = $value->$prop;
92
                }
93
                return $value;
94
            }
95
            return 'id' == $property ? $auth->getIdentity() : $auth->getUser()->$property;
96
        }
97
        return null;
98
    }
99
    
100
    /**
101
     * @param ControllerManager $controllerManager
102
     * @return \Auth\Controller\Plugin\Auth
103
     */
104
    public static function factory(ControllerManager $controllerManager)
105
    {
106
        return new static($controllerManager->getServiceLocator()->get('AuthenticationService'));
0 ignored issues
show
Documentation introduced by
$controllerManager->getS...AuthenticationService') is of type object|array, but the function expects a object<Auth\AuthenticationService>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
    }
108
}
109