Completed
Push — master ( 503636...a81f54 )
by Anton
09:53 queued 06:00
created

AuthProvider::setHybridauth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Hybrid Auth integration
4
 * @author yuklia <[email protected]>
5
 */
6
namespace Application\Auth;
7
8
use Application\Auth;
9
use Application\Users;
10
use Bluz\Application\Exception\ApplicationException;
11
use Bluz\Proxy\Config;
12
use Bluz\Proxy\Messages;
13
use Bluz\Proxy\Response;
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);
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