ResponseV2   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
ccs 15
cts 15
cp 1
wmc 6
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 14 5
A toArray() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\NoCaptcha\Utilities;
6
7
/**
8
 * Class     ResponseV2
9
 *
10
 * @package  Arcanedev\NoCaptcha\Utilities
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class ResponseV2 extends AbstractResponse
14
{
15
    /* -----------------------------------------------------------------
16
     |  Main Methods
17
     | -----------------------------------------------------------------
18
     */
19
20
    /**
21
     * Build the response from an array.
22
     *
23
     * @param  array $array
24
     *
25
     * @return \Arcanedev\NoCaptcha\Utilities\ResponseV2|mixed
26
     */
27 42
    public static function fromArray(array $array)
28
    {
29 42
        $hostname       = $array['hostname'] ?? null;
30 42
        $challengeTs    = $array['challenge_ts'] ?? null;
31 42
        $apkPackageName = $array['apk_package_name'] ?? null;
32
33 42
        if (isset($array['success']) && $array['success'] == true)
34 36
            return new static(true, [], $hostname, $challengeTs, $apkPackageName);
35
36 6
        if ( ! (isset($array['error-codes']) && is_array($array['error-codes'])))
37 6
            $array['error-codes'] = [ResponseV3::E_UNKNOWN_ERROR];
38
39 6
        return new static(false, $array['error-codes'], $hostname, $challengeTs, $apkPackageName);
40
    }
41
42
    /**
43
     * Convert the response object to array.
44
     *
45
     * @return array
46
     */
47 18
    public function toArray()
48
    {
49
        return [
50 18
            'success'          => $this->isSuccess(),
51 18
            'hostname'         => $this->getHostname(),
52 18
            'challenge_ts'     => $this->getChallengeTs(),
53 18
            'apk_package_name' => $this->getApkPackageName(),
54 18
            'error-codes'      => $this->getErrorCodes(),
55
        ];
56
    }
57
}
58