Completed
Pull Request — master (#79)
by ARCANEDEV
07:25
created

AbstractNoCaptcha   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 285
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 285
rs 10
c 0
b 0
f 0
ccs 52
cts 52
cp 1
wmc 16
lcom 2
cbo 4

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setSecret() 0 8 1
A getSiteKey() 0 4 1
A setSiteKey() 0 8 1
A setLang() 0 6 1
A setRequestClient() 0 6 1
A getLastResponse() 0 4 1
A verify() 0 8 1
A sendVerifyRequest() 0 9 1
parseResponse() 0 1 ?
A hasLang() 0 4 1
A checkKey() 0 8 1
A checkIsString() 0 8 2
A checkIsNotEmpty() 0 6 2
A toHtmlString() 0 4 1
1
<?php namespace Arcanedev\NoCaptcha;
2
3
use Arcanedev\NoCaptcha\Utilities\Request;
4
use Illuminate\Support\HtmlString;
5
6
/**
7
 * Class     AbstractNoCaptcha
8
 *
9
 * @package  Arcanedev\NoCaptcha
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
abstract class AbstractNoCaptcha implements Contracts\NoCaptcha
13
{
14
    /* -----------------------------------------------------------------
15
     |  Constants
16
     | -----------------------------------------------------------------
17
     */
18
19
    const CLIENT_URL   = 'https://www.google.com/recaptcha/api.js';
20
    const VERIFY_URL   = 'https://www.google.com/recaptcha/api/siteverify';
21
    const CAPTCHA_NAME = 'g-recaptcha-response';
22
23
    /* -----------------------------------------------------------------
24
     |  Properties
25
     | -----------------------------------------------------------------
26
     */
27
28
    /**
29
     * The shared key between your site and ReCAPTCHA
30
     *
31
     * @var string
32
     */
33
    protected $secret;
34
35
    /**
36
     * Your site key
37
     *
38
     * @var string
39
     */
40
    protected $siteKey;
41
42
    /**
43
     * Forces the widget to render in a specific language.
44
     * Auto-detects the user's language if unspecified.
45
     *
46
     * @var string
47
     */
48
    protected $lang;
49
50
    /**
51
     * HTTP Request Client
52
     *
53
     * @var \Arcanedev\NoCaptcha\Contracts\Utilities\Request
54
     */
55
    protected $request;
56
57
    /**
58
     * ReCaptcha's response.
59
     *
60
     * @var \Arcanedev\NoCaptcha\Utilities\ResponseV3|null
61
     */
62
    protected $response;
63
64
    /* -----------------------------------------------------------------
65
     |  Constructor
66
     | -----------------------------------------------------------------
67
     */
68
69
    /**
70
     * NoCaptcha constructor.
71
     *
72
     * @param  string       $secret
73
     * @param  string       $siteKey
74
     * @param  string|null  $lang
75
     */
76 200
    public function __construct($secret, $siteKey, $lang = null)
77
    {
78 200
        $this->setSecret($secret);
79 200
        $this->setSiteKey($siteKey);
80 200
        $this->setLang($lang);
81
82 200
        $this->setRequestClient(new Request);
83 200
    }
84
85
    /* -----------------------------------------------------------------
86
     |  Getters & Setters
87
     | -----------------------------------------------------------------
88
     */
89
90
    /**
91
     * Set the secret key.
92
     *
93
     * @param  string  $secret
94
     *
95
     * @return $this
96
     */
97 200
    protected function setSecret($secret)
98
    {
99 200
        self::checkKey('secret key', $secret);
100
101 200
        $this->secret = $secret;
102
103 200
        return $this;
104
    }
105
106
    /**
107
     * Get the site key.
108
     *
109
     * @return string
110
     */
111 16
    public function getSiteKey()
112
    {
113 16
        return $this->siteKey;
114
    }
115
116
    /**
117
     * Set Site key.
118
     *
119
     * @param  string  $siteKey
120
     *
121
     * @return $this
122
     */
123 200
    protected function setSiteKey($siteKey)
124
    {
125 200
        self::checkKey('site key', $siteKey);
126
127 200
        $this->siteKey = $siteKey;
128
129 200
        return $this;
130
    }
131
132
    /**
133
     * Set language code.
134
     *
135
     * @param  string  $lang
136
     *
137
     * @return $this
138
     */
139 200
    public function setLang($lang)
140
    {
141 200
        $this->lang = $lang;
142
143 200
        return $this;
144
    }
145
146
    /**
147
     * Set HTTP Request Client.
148
     *
149
     * @param  \Arcanedev\NoCaptcha\Contracts\Utilities\Request  $request
150
     *
151
     * @return $this
152
     */
153 72
    public function setRequestClient(Contracts\Utilities\Request $request)
154
    {
155 72
        $this->request = $request;
156
157 72
        return $this;
158
    }
159
160
    /**
161
     * Get the last response.
162
     *
163
     * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|null
164
     */
165 4
    public function getLastResponse()
166
    {
167 4
        return $this->response;
168
    }
169
170
    /* -----------------------------------------------------------------
171
     |  Main Methods
172
     | -----------------------------------------------------------------
173
     */
174
175
    /**
176
     * Verify Response.
177
     *
178
     * @param  string  $response
179
     * @param  string  $clientIp
180
     *
181
     * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|mixed
182
     */
183 32
    public function verify($response, $clientIp = null)
184
    {
185 32
        return $this->response = $this->sendVerifyRequest([
186 32
            'secret'   => $this->secret,
187 32
            'response' => $response,
188 32
            'remoteip' => $clientIp
189
        ]);
190
    }
191
192
    /**
193
     * Send verify request to API and get response.
194
     *
195
     * @param  array  $query
196
     *
197
     * @return \Arcanedev\NoCaptcha\Utilities\ResponseV3
198
     */
199 32
    protected function sendVerifyRequest(array $query = [])
200
    {
201 32
        $query = array_filter($query);
202 32
        $json  = $this->request->send(
203 32
            static::VERIFY_URL.'?'.http_build_query($query)
204
        );
205
206 32
        return $this->parseResponse($json);
207
    }
208
209
    /**
210
     * Parse the response.
211
     *
212
     * @param  string  $json
213
     *
214
     * @return \Arcanedev\NoCaptcha\Utilities\AbstractResponse|mixed
215
     */
216
    abstract protected function parseResponse($json);
217
218
    /* -----------------------------------------------------------------
219
     |  Check Methods
220
     | -----------------------------------------------------------------
221
     */
222
223
    /**
224
     * Check if has lang.
225
     *
226
     * @return bool
227
     */
228 36
    protected function hasLang()
229
    {
230 36
        return ! empty($this->lang);
231
    }
232
233
    /**
234
     * Check key.
235
     *
236
     * @param  string  $name
237
     * @param  string  $value
238
     */
239 200
    private static function checkKey($name, &$value)
240
    {
241 200
        self::checkIsString($name, $value);
242
243 200
        $value = trim($value);
244
245 200
        self::checkIsNotEmpty($name, $value);
246 200
    }
247
248
    /**
249
     * Check if the value is a string value.
250
     *
251
     * @param  string  $name
252
     * @param  string  $value
253
     *
254
     * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException
255
     */
256 200
    private static function checkIsString($name, $value)
257
    {
258 200
        if ( ! is_string($value)) {
259 16
            throw new Exceptions\ApiException(
260 16
                "The {$name} must be a string value, ".gettype($value).' given.'
261
            );
262
        }
263 200
    }
264
265
    /**
266
     * Check if the value is not empty.
267
     *
268
     * @param string  $name
269
     * @param string  $value
270
     *
271
     * @throws \Arcanedev\NoCaptcha\Exceptions\ApiException
272
     */
273 200
    private static function checkIsNotEmpty($name, $value)
274
    {
275 200
        if (empty($value)) {
276 16
            throw new Exceptions\ApiException("The {$name} must not be empty");
277
        }
278 200
    }
279
280
    /* -----------------------------------------------------------------
281
     |  Other Methods
282
     | -----------------------------------------------------------------
283
     */
284
285
    /**
286
     * Transform the string to an Html serializable object
287
     *
288
     * @param  string  $html
289
     *
290
     * @return \Illuminate\Support\HtmlString
291
     */
292 116
    protected function toHtmlString($html)
293
    {
294 116
        return new HtmlString($html);
295
    }
296
}
297