Completed
Push — develop ( ed809d...fdb9b4 )
by
unknown
39:31 queued 30:52
created

AuthenticationService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013-2015 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
namespace Auth;
10
11
use Zend\Authentication\AuthenticationService as ZendAuthService;
12
use Zend\Authentication\Adapter\AdapterInterface;
13
use Core\Repository\RepositoryInterface;
14
use Auth\Entity\AnonymousUser;
15
16
class AuthenticationService extends ZendAuthService
17
{
18
19
    /**
20
     * @var AnonymousUser
21
     */
22
    protected $user;
23
    /**
24
     * @var RepositoryInterface;
25
     */
26
    protected $repository;
27
28
    /**
29
     * @param RepositoryInterface $repository
30
     */
31
    public function __construct(RepositoryInterface $repository)
32
    {
33
        $this->setRepository($repository);
34
    }
35
36
    /**
37
     * @return RepositoryInterface
38
     */
39
    public function getRepository()
40
    {
41
        return $this->repository;
42
    }
43
44
    /**
45
     * @param RepositoryInterface $repository
46
     * @return $this
47
     */
48
    public function setRepository($repository)
49
    {
50
        $this->repository = $repository;
51
        return $this;
52
    }
53
54
    /**
55
     * @return AnonymousUser
56
     * @throws \OutOfBoundsException
57
     */
58
    public function getUser()
59
    {
60
        if (!$this->user) {
61
            if ($this->hasIdentity() && ($id = $this->getIdentity())) {
62
                $user = $this->getRepository()->find($id);
63
                if (!$user) {
64
                    throw new \OutOfBoundsException('Unknown user id: ' . $id);
65
                }
66
                $this->user = $user;
67
            } else {
68
                $this->user = new AnonymousUser();
69
            }
70
        }
71
72
        return $this->user;
73
    }
74
75
    /**
76
     * @param AdapterInterface $adapter
0 ignored issues
show
Documentation introduced by
Should the type for parameter $adapter not be null|AdapterInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
77
     * @return \Zend\Authentication\Result
78
     */
79
    public function authenticate(AdapterInterface $adapter = null)
80
    {
81
        $this->user = null; // clear user (especially guest user)
82
        return parent::authenticate($adapter);
83
    }
84
85
    public function clearIdentity()
86
    {
87
        parent::clearIdentity();
88
        $this->user = null;
89
90
        return $this;
91
    }
92
93
94
}
95