Completed
Pull Request — master (#20)
by Harry
11:39 queued 03:51
created

OAuth2Subscriber::error()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 16
cts 16
cp 1
rs 8.2222
cc 7
eloc 11
nc 4
nop 1
crap 7
1
<?php
2
/**
3
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Auth\OAuth2;
15
16
use GuzzleHttp\Event\BeforeEvent;
17
use GuzzleHttp\Event\ErrorEvent;
18
use GuzzleHttp\Event\RequestEvents;
19
use GuzzleHttp\Event\SubscriberInterface;
20
21
class OAuth2Subscriber implements SubscriberInterface
22
{
23
    const AUTH_NAME = 'gigya-oauth2';
24
25
    /** @var GrantInterface */
26
    private $grant;
27
    /** @var string|null */
28
    private $name;
29
30
    /**
31
     * @param GrantInterface $grant
32
     * @param string|null    $name
33
     */
34 8
    public function __construct(GrantInterface $grant, $name = null)
35
    {
36 8
        $this->grant = $grant;
37 8
        $this->name = $name ?: static::AUTH_NAME;
38 8
    }
39
40
    /**
41
     * Returns an array of event names this subscriber wants to listen to.
42
     *
43
     * @return array
44
     */
45 1
    public function getEvents()
46
    {
47
        return [
48 1
            'before' => ['sign', RequestEvents::SIGN_REQUEST],
49 1
            'error'  => ['error', RequestEvents::EARLY],
50 1
        ];
51
    }
52
53
    /**
54
     * Add the authentication params to the request.
55
     *
56
     * @param BeforeEvent $event
57
     */
58 1
    public function sign(BeforeEvent $event)
59
    {
60 1
        $request = $event->getRequest();
61 1
        if ($request->getScheme() == 'https'
62 1
            && $request->getConfig()->get('auth') == $this->name
63 1
        ) {
64 1
            $token = $this->grant->getToken();
65
66 1
            if (!is_null($token)) {
67 1
                $request->addHeader('Authorization', sprintf('OAuth %s', $token->getToken()));
68 1
            }
69 1
        }
70 1
    }
71
72
    /**
73
     * @param ErrorEvent $event
74
     */
75 4
    public function error(ErrorEvent $event)
76
    {
77 4
        $response = $event->getResponse();
78 4
        if ($response && $response->getStatusCode() == 401) {
79 3
            $request = $event->getRequest();
80 3
            if ($request->getScheme() == 'https'
81 3
                && $request->getConfig()->get('auth') == $this->name
82 3
                && !$request->getConfig()->get('retried')
83 3
            ) {
84 2
                $token = $this->grant->getToken();
85 2
                if (!is_null($token)) {
86 1
                    $request->getConfig()->set('retried', true);
87 1
                    $event->intercept($event->getClient()->send($request));
88 1
                }
89 2
            }
90 3
        }
91 4
    }
92
}
93