Response::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the godruoyi/laravel-tencent007-captcha.
5
 *
6
 * (c) Godruoyi <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled.
9
 */
10
11
namespace Godruoyi\Tencent007;
12
13
use Illuminate\Support\Arr;
14
15
class Response
16
{
17
    /**
18
     * The original value.
19
     *
20
     * @var mixed
21
     */
22
    protected $original;
23
24
    /**
25
     * Default level.
26
     *
27
     * @var int
28
     */
29
    protected $level = 0;
30
31
    /**
32
     * Default error message.
33
     *
34
     * @var string
35
     */
36
    protected $msg = 'Request failed';
37
38
    /**
39
     * Request has verfiy success.
40
     *
41
     * @var bool
42
     */
43
    protected $success = false;
44
45
    /**
46
     * Initialize Instance.
47
     *
48
     * @param mixed $result
49
     */
50 6
    public function __construct($result)
51
    {
52 6
        $this->parseResult($this->original = $result);
53 6
    }
54
55
    /**
56
     * Has success.
57
     *
58
     * @return bool
59
     */
60 2
    public function success()
61
    {
62 2
        return (bool) $this->success;
63
    }
64
65
    /**
66
     * Get message.
67
     *
68
     * @return string
69
     */
70 1
    public function message()
71
    {
72 1
        return (string) $this->msg;
73
    }
74
75
    /**
76
     * Get original value.
77
     *
78
     * @return mixed
79
     */
80 1
    public function getOriginal()
81
    {
82 1
        return $this->original;
83
    }
84
85
    /**
86
     * Get level.
87
     *
88
     * @return int
89
     */
90 1
    public function level($force = true)
91
    {
92 1
        if ($force) {
93 1
            return $this->success() ? $this->level : 0;
94
        }
95
96 1
        return $this->level;
97
    }
98
99
    /**
100
     * Parse response.
101
     *
102
     * @param array|null $result
103
     */
104 6
    protected function parseResult($result)
105
    {
106
        // use default setting
107 6
        if (!is_array($result)) {
108 2
            return;
109
        }
110
111 5
        $this->success = (1 === (int) Arr::get($result, 'response'));
112 5
        $this->level = (int) Arr::get($result, 'evil_level', '');
113 5
        $this->msg = (string) Arr::get($result, 'err_msg');
114 5
    }
115
}
116