AdapterChain::authenticate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace ZfcUser\Authentication\Adapter;
4
5
use Zend\Authentication\Adapter\AdapterInterface;
6
use Zend\Authentication\Result as AuthenticationResult;
7
use Zend\EventManager\Event;
8
use Zend\Stdlib\RequestInterface as Request;
9
use Zend\Stdlib\ResponseInterface as Response;
10
use ZfcBase\EventManager\EventProvider;
11
use ZfcUser\Exception;
12
13
class AdapterChain extends EventProvider implements AdapterInterface
14
{
15
    /**
16
     * @var AdapterChainEvent
17
     */
18
    protected $event;
19
20
    /**
21
     * Returns the authentication result
22
     *
23
     * @return AuthenticationResult
24
     */
25
    public function authenticate()
26
    {
27
        $e = $this->getEvent();
28
29
        $result = new AuthenticationResult(
30
            $e->getCode(),
31
            $e->getIdentity(),
32
            $e->getMessages()
33
        );
34
35
        $this->resetAdapters();
36
37
        return $result;
38
    }
39
40
    /**
41
     * prepareForAuthentication
42
     *
43
     * @param  Request $request
44
     * @return Response|bool
45
     * @throws Exception\AuthenticationEventException
46
     */
47
    public function prepareForAuthentication(Request $request)
48
    {
49
        $e = $this->getEvent();
50
        $e->setRequest($request);
51
52
        $this->getEventManager()->trigger('authenticate.pre', $e);
53
54
        $result = $this->getEventManager()->trigger('authenticate', $e, function ($test) {
55
            return ($test instanceof Response);
56
        });
57
58
        if ($result->stopped()) {
59
            if ($result->last() instanceof Response) {
60
                return $result->last();
61
            }
62
63
            throw new Exception\AuthenticationEventException(
64
                sprintf(
65
                    'Auth event was stopped without a response. Got "%s" instead',
66
                    is_object($result->last()) ? get_class($result->last()) : gettype($result->last())
67
                )
68
            );
69
        }
70
71
        if ($e->getIdentity()) {
72
            $this->getEventManager()->trigger('authenticate.success', $e);
73
            return true;
74
        }
75
76
        $this->getEventManager()->trigger('authenticate.fail', $e);
77
        return false;
78
    }
79
80
    /**
81
     * resetAdapters
82
     *
83
     * @return AdapterChain
84
     */
85
    public function resetAdapters()
86
    {
87
        $listeners = $this->getEventManager()->getListeners('authenticate');
88
        foreach ($listeners as $listener) {
89
            $listener = $listener->getCallback();
90
            if (is_array($listener) && $listener[0] instanceof ChainableAdapter) {
91
                $listener[0]->getStorage()->clear();
92
            }
93
        }
94
        return $this;
95
    }
96
97
    /**
98
     * logoutAdapters
99
     *
100
     * @return AdapterChain
101
     */
102
    public function logoutAdapters()
103
    {
104
        //Adapters might need to perform additional cleanup after logout
105
        $this->getEventManager()->trigger('logout', $this->getEvent());
106
    }
107
108
    /**
109
     * Get the auth event
110
     *
111
     * @return AdapterChainEvent
112
     */
113
    public function getEvent()
114
    {
115
        if (null === $this->event) {
116
            $this->setEvent(new AdapterChainEvent);
117
            $this->event->setTarget($this);
118
        }
119
        return $this->event;
120
    }
121
122
    /**
123
     * Set an event to use during dispatch
124
     *
125
     * By default, will re-cast to AdapterChainEvent if another event type is provided.
126
     *
127
     * @param  Event $e
128
     * @return AdapterChain
129
     */
130
    public function setEvent(Event $e)
131
    {
132
        if (!$e instanceof AdapterChainEvent) {
133
            $eventParams = $e->getParams();
134
            $e = new AdapterChainEvent();
135
            $e->setParams($eventParams);
136
        }
137
        $this->event = $e;
138
        return $this;
139
    }
140
}
141