Completed
Push — master ( 91361b...756e8e )
by max
02:53
created

Authenticator::authenticate()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 20
rs 9.4285
cc 3
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace T4web\Authentication\Service;
4
5
use Zend\Authentication\AuthenticationService;
6
use Zend\Authentication\Result as AuthResult;
7
use Zend\Authentication\Adapter\AdapterInterface;
8
use Zend\EventManager\EventManager;
9
use T4web\Authentication\AuthenticationEvent;
10
11
class Authenticator extends AuthenticationService
12
{
13
14
    /**
15
     * @var AuthenticationService
16
     */
17
    protected $authService;
18
19
    /**
20
     * @var AuthResult
21
     */
22
    protected $result;
23
24
    /**
25
     * @var AdapterInterface
26
     */
27
    protected $adapter;
28
29
    /**
30
     * @param AuthenticationService $authService
31
     * @param AdapterInterface $adapter
32
     */
33
    public function __construct(AuthenticationService $authService, AdapterInterface $adapter)
34
    {
35
        $this->authService = $authService;
36
        $this->adapter = $adapter;
37
    }
38
39
    /**
40
     * @param AdapterInterface|null $adapter
41
     * @return AuthResult
42
     */
43
    public function authenticate(AdapterInterface $adapter = null)
44
    {
45
        $event = new AuthenticationEvent();
46
        $event->setTarget($this);
47
48
        if (!$adapter) {
49
            $adapter = $this->getAdapter();
50
        }
51
52
        if ($adapter) {
53
            $event->setAdapter($adapter);
54
        }
55
56
        $eventManager = new EventManager();
57
        $eventManager->setIdentifiers(get_class($this));
58
59
        $eventManager->trigger($event);
60
61
        return $event->getResult();
62
    }
63
64
    /**
65
     * @return AdapterInterface
66
     */
67
    public function getAdapter()
68
    {
69
        return $this->adapter;
70
    }
71
72
    public function hasIdentity()
73
    {
74
        return $this->authService->hasIdentity();
75
    }
76
77
    public function logout()
78
    {
79
        $this->authService->clearIdentity();
80
    }
81
}
82