Completed
Pull Request — master (#248)
by Anton
06:12
created

AuthProvider   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 43.84%

Importance

Changes 19
Bugs 1 Features 1
Metric Value
c 19
b 1
f 1
dl 0
loc 205
rs 10
ccs 32
cts 73
cp 0.4384
wmc 24
lcom 1
cbo 11

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A getHybridauth() 0 8 2
A setHybridauth() 0 4 1
A setIdentity() 0 4 1
A getIdentity() 0 4 1
A getProviderName() 0 4 1
A setProviderName() 0 4 1
A getAuthAdapter() 0 13 3
A setAuthAdapter() 0 4 1
A registration() 0 14 2
B authProcess() 0 29 4
A getOptions() 0 4 1
A getAvailableProviders() 0 4 1
A alreadyRegisteredLogic() 0 11 2
A getProfile() 0 4 1
1
<?php
2
/**
3
 * Hybrid Auth integration
4
 * @author yuklia <[email protected]>
5
 */
6
namespace Application\Auth;
7
8
use Bluz\Application\Exception\ApplicationException;
9
use Bluz\Proxy\Config;
10
use Bluz\Proxy\Messages;
11
use Bluz\Proxy\Response;
12
use Application\Auth;
13
use Application\Users;
14
15
/**
16
 * Class AuthProvider
17
 * @package Application\Auth
18
 */
19
class AuthProvider implements AuthInterface
20
{
21
    /**
22
     * @var \Application\Users\Row $identity
23
     */
24
    protected $identity;
25
26
    /**
27
     * @var \Hybrid_Auth $hybridauth
28
     */
29
    protected $hybridauth;
30
31
    /**
32
     * @var \Hybrid_Provider_Adapter $authAdapter
33
     */
34
    protected $authAdapter;
35
36
    /**
37
     * the same name as was mentioned in hybridauth config section providers
38
     * @var string
39
     */
40
    protected $providerName;
41
42 2
    public function __construct($providerName)
43
    {
44 2
        if (!in_array(ucfirst($providerName), $this->getAvailableProviders())) {
45
            throw new ApplicationException(sprintf('Provider % is not defined
46
            in configuration file', ucfirst($providerName)));
47
        }
48 2
        $this->providerName = ucfirst($providerName);
49 2
    }
50
51
52
    /**
53
     * @return \Hybrid_Auth
54
     */
55
    public function getHybridauth()
56
    {
57
        if (!$this->hybridauth) {
58
            $this->hybridauth = new \Hybrid_Auth($this->getOptions());
59
        }
60
61
        return $this->hybridauth;
62
    }
63
64
    /**
65
     * @param \Hybrid_Auth $hybridauth
66
     */
67 2
    public function setHybridauth($hybridauth)
68
    {
69 2
        $this->hybridauth = $hybridauth;
70 2
    }
71
72
    /**
73
     * @param \Application\Users\Row $identity
74
     */
75 1
    public function setIdentity($identity)
76
    {
77 1
        $this->identity = $identity;
78 1
    }
79
80
    /**
81
     * @return \Application\Users\Row $user
82
     */
83
    public function getIdentity()
84
    {
85
        return $this->identity;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getProviderName()
92
    {
93
        return $this->providerName;
94
    }
95
96
    /**
97
     * @param string $providerName
98
     */
99
    public function setProviderName($providerName)
100
    {
101
        $this->providerName = $providerName;
102
    }
103
104
    /**
105
     * @return \Hybrid_Provider_Adapter
106
     * @throws \Exception
107
     */
108 2
    public function getAuthAdapter()
109
    {
110 2
        if (!$this->authAdapter) {
111
            /** @var \Hybrid_Provider_Adapter $authProvider */
112
            $this->authAdapter = $this->getHybridauth()->authenticate($this->providerName);
113
114
            if (!$this->authAdapter->isUserConnected()) {
115
                throw new \Exception('Cannot connect to current provider !');
116
            }
117
        }
118
119 2
        return $this->authAdapter;
120
    }
121
122
    /**
123
     * @param \Hybrid_Provider_Adapter $authAdapter
124
     */
125 2
    public function setAuthAdapter($authAdapter)
126
    {
127 2
        $this->authAdapter = $authAdapter;
128 2
    }
129
130
    /**
131
     * @param \Hybrid_User_Profile $data
132
     * @param  \Application\Users\Row $user
133
     * @return void
134
     */
135
    public function registration($data, $user)
136
    {
137
        $row = new Auth\Row();
138
        $row->userId = $user->id;
139
        $row->provider = strtolower($this->providerName);
140
        $row->foreignKey = $data->identifier;
141
        $row->token = $this->authAdapter->getAccessToken()['access_token'];
142
        $row->tokenSecret = ($this->authAdapter->getAccessToken()['access_token_secret']) ? : '';
143
        $row->tokenType = Auth\Table::TYPE_ACCESS;
144
        $row->save();
145
146
        Messages::addNotice(sprintf('Your account was linked to %s successfully !', $this->providerName));
0 ignored issues
show
Bug introduced by
The call to addNotice() misses a required argument $...$text.

This check looks for function calls that miss required arguments.

Loading history...
147
        Response::redirectTo('users', 'profile', ['id' => $user->id]);
148
    }
149
150
    /**
151
     * @return void
152
     */
153 2
    public function authProcess()
154
    {
155 2
        $this->authAdapter = $this->getAuthAdapter();
156 2
        $profile = $this->getProfile();
157
158
        /**
159
         * @var Auth\Table $authTable
160
         */
161 2
        $authTable = Auth\Table::getInstance();
162 2
        $auth = $authTable->getAuthRow(strtolower($this->providerName), $profile->identifier);
163
164
165 2
        if ($this->identity) {
166 1
            if ($auth) {
167 1
                Messages::addNotice(sprintf('You have already linked to %s', $this->providerName));
0 ignored issues
show
Bug introduced by
The call to addNotice() misses a required argument $...$text.

This check looks for function calls that miss required arguments.

Loading history...
168 1
                Response::redirectTo('users', 'profile', ['id' => $this->identity->id]);
169
            } else {
170
                $user = Users\Table::findRow($this->identity->id);
171
                $this->registration($profile, $user);
172
            }
173
        }
174
175 1
        if ($auth) {
176
            $this->alreadyRegisteredLogic($auth);
0 ignored issues
show
Compatibility introduced by
$auth of type object<Bluz\Auth\AbstractRow> is not a sub-type of object<Application\Auth\Row>. It seems like you assume a child class of the class Bluz\Auth\AbstractRow to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
177
        } else {
178 1
            Messages::addError(sprintf('First you need to be linked to %s', $this->providerName));
0 ignored issues
show
Bug introduced by
The call to addError() misses a required argument $...$text.

This check looks for function calls that miss required arguments.

Loading history...
179 1
            Response::redirectTo('users', 'signin');
180
        }
181
    }
182
183
    /**
184
     * @return array
185
     * @throws \Application\Exception
186
     */
187
    public function getOptions()
188
    {
189
        return Config::getData('hybridauth');
190
    }
191
192
    /**
193
     * @return array
194
     */
195 2
    public function getAvailableProviders()
196
    {
197 2
        return array_keys(Config::getData('hybridauth')['providers']);
198
    }
199
200
    /**
201
     * @param $auth
202
     * @return mixed
203
     */
204
    public function alreadyRegisteredLogic($auth)
205
    {
206
        $user = Users\Table::findRow($auth->userId);
207
208
        if ($user->status != Users\Table::STATUS_ACTIVE) {
209
            Messages::addError('User is not active');
0 ignored issues
show
Bug introduced by
The call to addError() misses a required argument $...$text.

This check looks for function calls that miss required arguments.

Loading history...
210
        }
211
212
        $user->tryLogin();
213
        Response::redirectTo('index', 'index');
214
    }
215
216
    /**
217
     * @return \Hybrid_User_Profile
218
     */
219 2
    public function getProfile()
220
    {
221 2
        return $this->authAdapter->getUserProfile();
222
    }
223
}
224