Passed
Push — master ( 734ccc...57db64 )
by Dāvis
02:59
created

DraugiemOAuth2Client::redirect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 3
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Oauth\Client\Client;
4
5
use Sludio\HelperBundle\Oauth\Client\OAuth2Client;
6
use Sludio\HelperBundle\Oauth\Client\Provider\Draugiem\DraugiemUser;
7
use Symfony\Component\HttpFoundation\RedirectResponse;
8
9
class DraugiemOAuth2Client extends OAuth2Client
10
{
11
    const METHOD = 'POST';
12
13
    protected $isStateless = true;
14
15
    public function redirect(array $scopes = [], array $options = [], $token = null)
16
    {
17
        $data = [
18
            'hash' => md5($this->provider->getClientSecret().$this->provider->getRedirectUri()),
19
            'redirect' => $this->provider->getRedirectUri(),
20
            'app' => $this->provider->getClientId(),
21
        ];
22
23
        $url = $this->provider->getBaseAuthorizationUrl().'?'.http_build_query($data);
24
25
        if (!$this->isStateless) {
26
            $this->getSession()->set(self::OAUTH2_SESSION_STATE_KEY, $this->provider->getState());
27
        }
28
29
        return new RedirectResponse($url);
30
    }
31
32
    public function fetchUser(array $attributes = [])
33
    {
34
        $user = $this->returnRedirect();
35
36
        return new DraugiemUser($user);
37
    }
38
39
    public function returnRedirect(array $scopes = [], array $options = [])
40
    {
41
        if (!empty($scopes)) {
42
            $options['scope'] = $scopes;
43
        }
44
45
        $data = [
46
            'app' => $this->provider->getClientSecret(),
47
            'code' => $this->getCurrentRequest()->get('dr_auth_code'),
48
            'action' => 'authorize',
49
        ];
50
51
        $url = $this->provider->getBaseAccessTokenUrl().'?'.http_build_query($data);
52
53
        $factory = $this->provider->getRequestFactory();
54
        $request = $factory->getRequestWithOptions(static::METHOD, $url, $data);
55
56
        return $this->provider->getParsedResponse($request);
57
    }
58
}
59