Response::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 2
1
<?php
2
namespace Graceland\SafeInCloud;
3
4
use GuzzleHttp\Message\ResponseInterface;
5
6
class Response
7
{
8
    protected $body;
9
    protected $encrypter;
10
    protected $request;
11
    protected $response;
12
13
    public function __construct(ResponseInterface $response, Request $request)
14
    {
15
        $this->request   = $request;
16
        $this->response  = $response;
17
        $this->encrypter = $this->request->getEncrypter();
18
        $this->body      = $this->response->json();
19
    }
20
21
    /**
22
     * @return mixed
23
    */
24
    public function get($key, $default = null)
25
    {
26
        if ($this->has($key) === false) {
27
            return $default;
28
        }
29
30
        return $this->body[$key];
31
    }
32
33
    /**
34
     * @return mixed
35
    */
36
    public function getDecrypted($key, $default = null, $assocKeys = [])
37
    {
38
        $value = $this->get($key, $default);
39
40
        if ($value === $default) {
41
            return $default;
42
        }
43
44
        return $this->decrypt($value, null, $assocKeys);
45
    }
46
47
    public function has($key)
48
    {
49
        return (isset($this->body[$key]) === true);
50
    }
51
52
    public function isSuccessful()
53
    {
54
        return (isset($this->body['success']) === true && $this->body['success'] === true);
55
    }
56
57
    public function isValid()
58
    {
59
        if ($this->response->getStatusCode() < 200 || $this->response->getStatusCode() >= 300) {
60
            return false;
61
        }
62
63
        if (isset($this->body['nonce']) === false || isset($this->body['verifier']) === false) {
64
            return false;
65
        }
66
67
        $nonce    = base64_decode($this->body['nonce']);
68
        $verifier = $this->decrypt($this->body['verifier'], $nonce);
69
70
        return $verifier === $this->body['nonce'];
71
    }
72
73
    /**
74
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
75
    */
76
    protected function decrypt($data, $nonce = null, $assocKeys = [])
77
    {
78
        if ($nonce === null) {
79
            $nonce = $this->request->getNonce();
80
        }
81
82
        if (is_array($data) === false) {
83
            return $this->encrypter->decrypt(base64_decode($data), $nonce);
84
        }
85
86
        $decrypted = [];
87
        $isAssoc   = (bool) count(array_filter(array_keys($data), 'is_string'));
88
89
        foreach ($data as $key => $value) {
90
            if ($isAssoc === true and in_array($key, $assocKeys) === false) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
91
                $decrypted[$key] = $value;
92
                continue;
93
            }
94
95
            $decrypted[$key] = $this->decrypt($value, $nonce, $assocKeys);
96
        }
97
98
        return $decrypted;
99
    }
100
}
101