Completed
Pull Request — master (#247)
by
unknown
09:38
created

AuthProvider   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 44.87%

Importance

Changes 18
Bugs 1 Features 1
Metric Value
c 18
b 1
f 1
dl 0
loc 217
rs 10
ccs 35
cts 78
cp 0.4487
wmc 26
lcom 1
cbo 11

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setHybridauth() 0 4 1
A setResponse() 0 4 1
A getResponse() 0 4 1
A setIdentity() 0 4 1
A getIdentity() 0 4 1
A getProviderName() 0 4 1
A setProviderName() 0 4 1
A setAuthAdapter() 0 4 1
A registration() 0 15 2
A getOptions() 0 4 1
A getAvailableProviders() 0 4 1
A getProfile() 0 4 1
A getHybridauth() 0 8 2
A getAuthAdapter() 0 13 3
B authProcess() 0 29 4
A alreadyRegisteredLogic() 0 11 2
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 Application\Auth;
12
use Application\Users;
13
14
/**
15
 * Class AuthProvider
16
 * @package Application\Auth
17
 */
18
class AuthProvider implements AuthInterface
19
{
20
    /** @var \Application\Bootstrap */
21
    protected $response;
22
23
    /** @var \Application\Users\Row $identity */
24
    protected $identity;
25
26
    /** @var \Hybrid_Auth $hybridauth */
27
    protected $hybridauth;
28
29
    /** @var \Hybrid_Provider_Adapter $authAdapter */
30
    protected $authAdapter;
31
32
    /**
33
     * the same name as was mentioned in hybridauth config section providers
34
     * @var string
35
     */
36
    protected $providerName;
37
38 2
    public function __construct($providerName)
39
    {
40 2
        if (!in_array(ucfirst($providerName), $this->getAvailableProviders())) {
41
            throw new ApplicationException(sprintf('Provider % is not defined
42
            in configuration file', ucfirst($providerName)));
43
        }
44 2
        $this->providerName = ucfirst($providerName);
45 2
    }
46
47
48
    /**
49
     * @return \Hybrid_Auth
50
     */
51
    public function getHybridauth()
52
    {
53
        if (!$this->hybridauth) {
54
            $this->hybridauth = new \Hybrid_Auth($this->getOptions());
55
        }
56
57
        return $this->hybridauth;
58
    }
59
60 2
    public function setHybridauth($hybridauth)
61
    {
62 2
        $this->hybridauth = $hybridauth;
63 2
    }
64
65
66
    /**
67
     * @param \Bluz\Application\Application $response
68
     */
69 2
    public function setResponse($response)
70
    {
71 2
        $this->response = $response;
0 ignored issues
show
Documentation Bug introduced by
$response is of type object<Bluz\Application\Application>, but the property $response was declared to be of type object<Application\Bootstrap>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
72 2
    }
73
74
    /**
75
     * @return \Application\Bootstrap
76
     */
77
    public function getResponse()
78
    {
79
        return $this->response;
80
    }
81
82
    /**
83
     * @param \Application\Users\Row $identity
84
     */
85 1
    public function setIdentity($identity)
86
    {
87 1
        $this->identity = $identity;
88 1
    }
89
90
    /**
91
     * @return \Application\Users\Row $user
92
     */
93
    public function getIdentity()
94
    {
95
        return $this->identity;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getProviderName()
102
    {
103
        return $this->providerName;
104
    }
105
106
    /**
107
     * @param string $providerName
108
     */
109
    public function setProviderName($providerName)
110
    {
111
        $this->providerName = $providerName;
112
    }
113
114
    /**
115
     * @return \Hybrid_Provider_Adapter
116
     * @throws \Exception
117
     */
118 2
    public function getAuthAdapter()
119
    {
120 2
        if (!$this->authAdapter) {
121
            /** @var \Hybrid_Provider_Adapter $authProvider */
122
            $this->authAdapter = $this->getHybridauth()->authenticate($this->providerName);
123
124
            if (!$this->authAdapter->isUserConnected()) {
125
                throw new \Exception('Cannot connect to current provider !');
126
            }
127
        }
128
129 2
        return $this->authAdapter;
130
    }
131
132
    /**
133
     * @param \Hybrid_Provider_Adapter $authAdapter
134
     */
135 2
    public function setAuthAdapter($authAdapter)
136
    {
137 2
        $this->authAdapter = $authAdapter;
138 2
    }
139
140
    /**
141
     * @param \Hybrid_User_Profile $data
142
     * @param  \Application\Users\Row $user
143
     * @return void
144
     */
145
    public function registration($data, $user)
146
    {
147
        $row = new Auth\Row();
148
        $row->userId = $user->id;
149
        $row->provider = strtolower($this->providerName);
150
151
        $row->foreignKey = $data->identifier;
152
        $row->token = $this->authAdapter->getAccessToken()['access_token'];
153
        $row->tokenSecret = ($this->authAdapter->getAccessToken()['access_token_secret']) ? : '';
154
        $row->tokenType = Auth\Table::TYPE_ACCESS;
155
        $row->save();
156
157
        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...
158
        $this->response->redirectTo('users', 'profile', ['id' => $user->id]);
159
    }
160
161
    /**
162
     * @return void
163
     */
164 2
    public function authProcess()
165
    {
166 2
        $this->authAdapter = $this->getAuthAdapter();
167 2
        $profile = $this->getProfile();
168
169
        /**
170
         * @var Auth\Table $authTable
171
         */
172 2
        $authTable = Auth\Table::getInstance();
173 2
        $auth = $authTable->getAuthRow(strtolower($this->providerName), $profile->identifier);
174
175
176 2
        if ($this->identity) {
177 1
            if ($auth) {
178 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...
179 1
                $this->response->redirectTo('users', 'profile', ['id' => $this->identity->id]);
180
            } else {
181
                $user = Users\Table::findRow($this->identity->id);
182
                $this->registration($profile, $user);
183
            }
184
        }
185
186 1
        if ($auth) {
187
            $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...
188
        } else {
189 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...
190 1
            $this->response->redirectTo('users', 'signin');
191
        }
192
    }
193
194
    /**
195
     * @return array
196
     * @throws \Application\Exception
197
     */
198
    public function getOptions()
199
    {
200
        return Config::getData('hybridauth');
201
    }
202
203
    /**
204
     * @return array
205
     */
206 2
    public function getAvailableProviders()
207
    {
208 2
        return array_keys(Config::getData('hybridauth')['providers']);
209
    }
210
211
    /**
212
     * @param $auth
213
     * @return mixed
214
     */
215
    public function alreadyRegisteredLogic($auth)
216
    {
217
        $user = Users\Table::findRow($auth->userId);
218
219
        if ($user->status != Users\Table::STATUS_ACTIVE) {
220
            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...
221
        }
222
223
        $user->tryLogin();
224
        $this->response->redirectTo('index', 'index');
225
    }
226
227
    /**
228
     * @return \Hybrid_User_Profile
229
     */
230 2
    public function getProfile()
231
    {
232 2
        return $this->authAdapter->getUserProfile();
233
    }
234
}
235