1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Authy Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Authy Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Rinvex\Authy; |
17
|
|
|
|
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
19
|
|
|
|
20
|
|
|
class Response |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The raw Http response instance. |
24
|
|
|
* |
25
|
|
|
* @var \Psr\Http\Message\ResponseInterface |
26
|
|
|
*/ |
27
|
|
|
protected $httpResponse; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Create a new Authy response instance. |
31
|
|
|
* |
32
|
|
|
* @param \Psr\Http\Message\ResponseInterface $httpResponse |
33
|
|
|
*/ |
34
|
|
|
public function __construct(ResponseInterface $httpResponse) |
35
|
|
|
{ |
36
|
|
|
$this->httpResponse = $httpResponse; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get Authy response status code. |
41
|
|
|
* |
42
|
|
|
* @return int |
43
|
|
|
*/ |
44
|
|
|
public function statusCode() |
45
|
|
|
{ |
46
|
|
|
return $this->httpResponse->getStatusCode(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return Authy response body as array. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function body() |
55
|
|
|
{ |
56
|
|
|
return json_decode($this->httpResponse->getBody(), true) ?: []; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get Authy response body item. |
61
|
|
|
* |
62
|
|
|
* @param string $var |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
public function get($var) |
67
|
|
|
{ |
68
|
|
|
$body = $this->body(); |
69
|
|
|
|
70
|
|
|
return isset($body[$var]) ? $body[$var] : null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get Authy response body message. |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function message() |
79
|
|
|
{ |
80
|
|
|
return $this->get('message'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Determine if the Authy response succeed. |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
public function succeed() |
89
|
|
|
{ |
90
|
|
|
return $this->statusCode() == 200 && $this->isSuccess($this->get('success')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Determine if the Authy response failed. |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function failed() |
99
|
|
|
{ |
100
|
|
|
return ! $this->succeed(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get Authy response errors. |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function errors() |
109
|
|
|
{ |
110
|
|
|
$errors = $this->get('errors') ?: []; |
111
|
|
|
|
112
|
|
|
return $this->failed() && ! empty($errors) ? $errors : []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Determine if the given result is success. |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
protected function isSuccess($result) |
121
|
|
|
{ |
122
|
|
|
return ! is_null($result) ? (is_string($result) && $result == 'true') || (is_bool($result) && $result) : false; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|