Completed
Push — develop ( dfd31d...f6e11d )
by
unknown
25:04 queued 17:04
created

OAuth::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
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 * @author    [email protected]
9
 */
10
11
namespace Auth\Controller\Plugin;
12
13
use Zend\ServiceManager\ServiceLocatorInterface;
14
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
15
use Zend\Mvc\Controller\PluginManager as ControllerManager;
16
17
/**
18
 * Class OAuth
19
 * creates and revokes permanent Sessions
20
 * this instance cannot be shared, but since Sessions are maintained by hybridAuth, there is also no need to
21
 * @package Auth\Controller\Plugin
22
 */
23
class OAuth extends AbstractPlugin
24
{
25
    /**
26
     * @var ServiceLocatorInterface
27
     */
28
    protected $serviceManager;
29
30
    protected $user;
31
32
    protected $providerKey;
33
34
    protected $adapter;
35
36
    /**
37
     * @param ServiceLocatorInterface $serviceManager
38
     */
39
    public function __construct(ServiceLocatorInterface $serviceManager)
40
    {
41
        $this->serviceManager = $serviceManager;
42
    }
43
    
44
    public function setUser($user)
45
    {
46
        if (!empty($this->user)) {
47
            throw new \RuntimeException('User for oAuth cannot be changed, once the Authentification has been etablished');
48
        }
49
        $this->user = $user;
50
        return $this;
51
    }
52
53
    public function getUser()
54
    {
55
        $user = $this->user;
56
        // @TODO check on type
57
        if (empty($user)) {
58
            $controller = $this->getController();
59
            $user = $controller->auth()->getUser();
60
            $this->setUser($user);
61
        }
62
        return $user;
63
    }
64
65
    public function getHybridAuth()
66
    {
67
        return $this->serviceManager->get('HybridAuth');
68
    }
69
70
    /**
71
     * @param $providerKey
72
     * @param null $user
73
     * @return $this
74
     */
75
    public function __invoke($providerKey, $user = null)
76
    {
77
        if (!empty($user)) {
78
            $this->setUser($user);
79
        }
80
        $this->providerKey = $providerKey;
81
        return $this;
82
    }
83
84
    /**
85
     * for backend there is only one possibility to get a connection,
86
     * and that is by stored Session
87
     * @return bool
88
     */
89
    public function isAvailable()
90
    {
91
        if (!empty($this->adapter)) {
92
            // adapter is already etablished
93
            return true;
94
        }
95
        $user = $this->getUser();
96
        $sessionDataStored = $user->getAuthSession($this->providerKey);
97
        if (empty($sessionDataStored)) {
98
            // for this user no session has been stored
99
            return false;
100
        }
101
        $hybridAuth = $this->getHybridAuth();
102
        $hybridAuth->restoreSessionData($sessionDataStored);
103
        if ($hybridAuth->isConnectedWith($this->providerKey)) {
104
            return true;
105
        }
106
        return false;
107
    }
108
109
    /**
110
     * everything relevant is happening here, included the interactive registration
111
     * if the User already has a session, it is retrieved
112
     */
113
    public function getAdapter()
114
    {
115
        if (empty($this->adapter)) {
116
            $user = $this->getUser();
117
            $sessionDataStored = $user->getAuthSession($this->providerKey);
118
            $hybridAuth = $this->getHybridAuth();
119
            if (!empty($sessionDataStored)) {
120
                $hybridAuth->restoreSessionData($sessionDataStored);
121
            }
122
            $adapter = $hybridAuth->authenticate($this->providerKey);
123
            $sessionData    = $hybridAuth->getSessionData();
124
            if ($sessionData != $sessionDataStored) {
125
                $user->updateAuthSession($this->providerKey, $sessionData);
126
            }
127
            $this->adapter = $adapter;
128
        }
129
        return $this->adapter;
130
    }
131
132
    /**
133
     * logout and clears the stored Session,
134
     */
135
    public function sweepProvider()
136
    {
137
        $user = $this->getUser();
138
        $hybridAuth = $this->getHybridAuth();
139
        // first test, if there is a connection at all
140
        // that prevents an authentification just for to logout
141
        if ($hybridAuth->isConnectedWith($this->providerKey)) {
142
            $this->getAdapter($this->providerKey)->logout();
0 ignored issues
show
Unused Code introduced by
The call to OAuth::getAdapter() has too many arguments starting with $this->providerKey.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
143
        }
144
        $user->removeSessionData($this->providerKey);
145
        unset($this->adapter);
146
        return $this;
147
    }
148
    
149
    /**
150
     * @param ControllerManager $controllerManager
151
     * @return OAuth
152
     */
153
    public static function factory(ControllerManager $controllerManager)
154
    {
155
        return new static($controllerManager->getServiceLocator());
156
    }
157
}
158