GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RequestBuilder   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 149
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A buildGuzzleRequest() 0 22 2
A addCallable() 0 8 2
A executeCallableChain() 0 13 3
A buildForVersionFourOrFive() 0 16 2
A buildForVersionSix() 0 14 2
A buildForVersionThree() 0 15 3
1
<?php
2
/*
3
 * This file is part of the ReCaptcha Library.
4
 *
5
 * (c) Ilya Pokamestov <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
*/
10
11
namespace DS\Library\ReCaptcha\Http\Client\Guzzle;
12
13
use Psr\Http\Message\RequestInterface;
14
15
/**
16
 * ReCaptcha library, Guzzle request builder, build request object for any Guzzle versions.
17
 *
18
 * @author Ilya Pokamestov <[email protected]>
19
 *
20
 * Class RequestBuilder
21
 * @package DS\Library\ReCaptcha\Http\Client\Guzzle
22
 */
23
class RequestBuilder
24
{
25
    /** @var string */
26
    protected $guzzlePrsRequestClass = '\GuzzleHttp\Psr7\Request';
27
    /** @var GuzzleClient */
28
    protected $guzzleReCaptchaClient;
29
    /** @var \Guzzle\Http\Client|\GuzzleHttp\Client */
30
    protected $guzzleClient;
31
    /** @var array */
32
    protected $callableChain = array();
33
34
    /**
35
     * @param GuzzleClient $guzzle
36
     */
37
    public function __construct(GuzzleClient $guzzle)
38
    {
39
        $this->guzzleReCaptchaClient = $guzzle;
40
        $this->guzzleClient = $guzzle->getGuzzleClient();
41
    }
42
43
    /**
44
     * Build guzzle request based on Guzzle client version.
45
     *
46
     * @param RequestInterface $request
47
     * @return mixed
48
     * @throws RequestException
49
     */
50
    public function buildGuzzleRequest(RequestInterface $request)
51
    {
52
        $this->addCallable(function () use ($request) {
53
            return $this->buildForVersionThree($request);
54
        });
55
56
        $this->addCallable(function () use ($request) {
57
            return $this->buildForVersionFourOrFive($request);
58
        });
59
60
        $this->addCallable(function () use ($request) {
61
            return $this->buildForVersionSix($request);
62
        });
63
64
        $guzzleRequest = $this->executeCallableChain();
65
66
        if (null === $guzzleRequest) {
67
            throw new RequestException('Unknown Guzzle client, request instance can not be created.');
68
        }
69
70
        return $guzzleRequest;
71
    }
72
73
    /**
74
     * Add request builder callable
75
     *
76
     * @param callable $callable
77
     */
78
    protected function addCallable($callable)
79
    {
80
        if (!is_callable($callable)) {
81
            throw new \InvalidArgumentException('Argument must be callable.');
82
        }
83
84
        $this->callableChain[] = $callable;
85
    }
86
87
    /**
88
     * Execute callable chain and return callable result.
89
     *
90
     * @return mixed
91
     */
92
    protected function executeCallableChain()
93
    {
94
        $callableResult = null;
95
        foreach ($this->callableChain as $callable) {
96
            $callableResult = call_user_func($callable);
97
98
            if (null !== $callableResult) {
99
                break;
100
            }
101
        }
102
103
        return $callableResult;
104
    }
105
106
    /**
107
     * Build request object for Guzzle 4.* or 5.*
108
     *
109
     * @param RequestInterface $request
110
     * @return mixed
111
     */
112
    protected function buildForVersionFourOrFive(RequestInterface $request)
113
    {
114
        if (method_exists($this->guzzleClient, 'createRequest')) {
115
            $this->guzzleReCaptchaClient->setVersion(Version::VERSION_FOUR_FIVE);
116
            return $this->guzzleClient->createRequest(
117
                $request->getMethod(),
118
                (string)$request->getUri(),
119
                array (
120
                    'headers' => $request->getHeaders(),
121
                    'body' => $request->getBody()->getContents(),
122
                )
123
            );
124
        }
125
126
        return null;
127
    }
128
129
    /**
130
     * Build request object for Guzzle 6.*
131
     *
132
     * @param RequestInterface $request
133
     * @return mixed
134
     */
135
    protected function buildForVersionSix(RequestInterface $request)
136
    {
137
        if (class_exists($this->guzzlePrsRequestClass)) {
138
            $this->guzzleReCaptchaClient->setVersion(Version::VERSION_SIX);
139
            return new $this->guzzlePrsRequestClass(
140
                $request->getMethod(),
141
                (string)$request->getUri(),
142
                $request->getHeaders(),
143
                $request->getBody()
144
            );
145
        }
146
147
        return null;
148
    }
149
150
    /**
151
     * Build request object for Guzzle 3.*
152
     *
153
     * @param RequestInterface $request
154
     * @return mixed
155
     */
156
    protected function buildForVersionThree(RequestInterface $request)
157
    {
158
        if (get_class($this->guzzleClient) === GuzzleClient::GUZZLE_THREE_CLIENT_CLASS
159
            && method_exists($this->guzzleClient, 'createRequest')) {
160
            $this->guzzleReCaptchaClient->setVersion(Version::VERSION_THREE);
161
            return $this->guzzleClient->createRequest(
162
                $request->getMethod(),
163
                (string)$request->getUri(),
164
                $request->getHeaders(),
165
                $request->getBody()->getContents()
166
            );
167
        }
168
169
        return null;
170
    }
171
}
172