Completed
Push — master ( 2d123f...e5997e )
by Abdelrahman
01:43
created

Response::body()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
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
declare(strict_types=1);
17
18
namespace Rinvex\Authy;
19
20
use Psr\Http\Message\ResponseInterface;
21
22
class Response
23
{
24
    /**
25
     * The Aythy response body content as array.
26
     *
27
     * @var array
28
     */
29
    protected $body;
30
31
    /**
32
     * The Aythy response status code.
33
     *
34
     * @var int
35
     */
36
    protected $status;
37
38
    /**
39
     * Create a new Authy response instance.
40
     *
41
     * @param \Psr\Http\Message\ResponseInterface $httpResponse
42
     */
43
    public function __construct(ResponseInterface $httpResponse)
44
    {
45
        $this->status = $httpResponse->getStatusCode();
46
        $this->body = ($body = (string) $httpResponse->getBody()) ? json_decode($body, true) : null;
47
    }
48
49
    /**
50
     * Get Authy response status code.
51
     *
52
     * @return int
53
     */
54
    public function statusCode(): int
55
    {
56
        return $this->status;
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
        return $this->body[$var] ?? null;
69
    }
70
71
    /**
72
     * Get Authy response body message.
73
     *
74
     * @return string
75
     */
76
    public function message()
77
    {
78
        return $this->get('message');
79
    }
80
81
    /**
82
     * Determine if the Authy response succeed.
83
     *
84
     * @return bool
85
     */
86
    public function succeed(): bool
87
    {
88
        return $this->statusCode() === 200 && $this->isSuccess($this->get('success'));
89
    }
90
91
    /**
92
     * Determine if the Authy response failed.
93
     *
94
     * @return bool
95
     */
96
    public function failed(): bool
97
    {
98
        return ! $this->succeed();
99
    }
100
101
    /**
102
     * Get Authy response errors.
103
     *
104
     * @return array
105
     */
106
    public function errors()
107
    {
108
        $errors = $this->get('errors') ?: [];
109
110
        return $this->failed() && ! empty($errors) ? $errors : [];
111
    }
112
113
    /**
114
     * Determine if the given result is success.
115
     *
116
     * @return bool
117
     */
118
    protected function isSuccess($result): bool
119
    {
120
        return ! is_null($result) ? (is_string($result) && $result === 'true') || (is_bool($result) && $result) : false;
121
    }
122
}
123