ProviderRedirectTrait   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 112
ccs 31
cts 31
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A followRequestRedirects() 0 21 3
getHttpClient() 0 1 ?
A getRedirectLimit() 0 4 1
A isRedirect() 0 6 3
A getResponse() 0 10 2
A setRedirectLimit() 0 14 3
1
<?php
2
3
namespace Stevenmaguire\OAuth2\Client\Tool;
4
5
use GuzzleHttp\Exception\BadResponseException;
6
use GuzzleHttp\Psr7\Uri;
7
use InvalidArgumentException;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
trait ProviderRedirectTrait
12
{
13
    /**
14
     * Maximum number of times to follow provider initiated redirects
15
     *
16
     * @var integer
17
     */
18
    protected $redirectLimit = 2;
19
20
    /**
21
     * Retrieves a response for a given request and retrieves subsequent
22
     * responses, with authorization headers, if a redirect is detected.
23
     *
24
     * @param  RequestInterface $request
25
     * @return ResponseInterface
26
     * @throws BadResponseException
27
     */
28 8
    protected function followRequestRedirects(RequestInterface $request)
29
    {
30 8
        $response = null;
31 8
        $attempts = 0;
32
33 8
        while ($attempts < $this->redirectLimit) {
34 8
            $attempts++;
35 8
            $response = $this->getHttpClient()->send($request, [
36
                'allow_redirects' => false
37 8
            ]);
38
39 6
            if ($this->isRedirect($response)) {
40 2
                $redirectUrl = new Uri($response->getHeader('Location')[0]);
41 2
                $request = $request->withUri($redirectUrl);
42 1
            } else {
43 4
                break;
44
            }
45 1
        }
46
47 6
        return $response;
48
    }
49
50
    /**
51
     * Returns the HTTP client instance.
52
     *
53
     * @return GuzzleHttp\ClientInterface
54
     */
55
    abstract public function getHttpClient();
56
57
    /**
58
     * Retrieves current redirect limit.
59
     *
60
     * @return integer
61
     */
62 4
    public function getRedirectLimit()
63
    {
64 4
        return $this->redirectLimit;
65
    }
66
67
    /**
68
     * Determines if a given response is a redirect.
69
     *
70
     * @param  ResponseInterface  $response
71
     *
72
     * @return boolean
73
     */
74 6
    protected function isRedirect(ResponseInterface $response)
75
    {
76 6
        $statusCode = $response->getStatusCode();
77
78 6
        return $statusCode > 300 && $statusCode < 400 && $response->hasHeader('Location');
79
    }
80
81
    /**
82
     * Sends a request instance and returns a response instance.
83
     *
84
     * WARNING: This method does not attempt to catch exceptions caused by HTTP
85
     * errors! It is recommended to wrap this method in a try/catch block.
86
     *
87
     * @param  RequestInterface $request
88
     * @return ResponseInterface
89
     */
90 8
    public function getResponse(RequestInterface $request)
91
    {
92
        try {
93 8
            $response = $this->followRequestRedirects($request);
94 5
        } catch (BadResponseException $e) {
95 2
            $response = $e->getResponse();
96
        }
97
98 8
        return $response;
99
    }
100
101
    /**
102
     * Updates the redirect limit.
103
     *
104
     * @param integer $limit
105
     * @return League\OAuth2\Client\Provider\AbstractProvider
106
     * @throws InvalidArgumentException
107
     */
108 14
    public function setRedirectLimit($limit)
109
    {
110 14
        if (!is_int($limit)) {
111 4
            throw new InvalidArgumentException('redirectLimit must be an integer.');
112
        }
113
114 10
        if ($limit < 1) {
115 4
            throw new InvalidArgumentException('redirectLimit must be greater than or equal to one.');
116
        }
117
118 6
        $this->redirectLimit = $limit;
119
120 6
        return $this;
121
    }
122
}
123