Completed
Pull Request — master (#79)
by ARCANEDEV
04:20
created

ResponseV2::fromArray()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

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