Helpers::returnResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace CodeBlog\JWT;
4
5
/**
6
 * Class CodeBlog Helpers
7
 *
8
 * @author Whallysson Avelino <https://github.com/whallysson>
9
 * @package CodeBlog\JWT
10
 */
11
12
class Helpers
13
{
14
15
    /**
16
     * Encode a JSON string to a base64 Url string
17
     *
18
     * @param string $jsonTokenString
19
     *
20
     * @return string
21
     */
22
    public static function encode(string $jsonTokenString): string
23
    {
24
        return str_replace('=', '', strtr(base64_encode($jsonTokenString), '+/', '-_'));
25
    }
26
27
    /**
28
     * Decode a base64 Url string to a JSON string
29
     *
30
     * @param string $base64UrlString
31
     *
32
     * @return string
33
     */
34
    public static function decode(string $base64UrlString): string
35
    {
36
        $remainder = strlen($base64UrlString) % 4;
37
        if ($remainder) {
38
            $base64UrlString .= str_repeat('=', 4 - $remainder);
39
        }
40
41
        return base64_decode(strtr($base64UrlString, '-_', '+/'));
42
    }
43
44
    /**
45
     * @param int $code
46
     * @param $data
47
     */
48
    public static function returnResponse(int $code, $data): void
49
    {
50
        header('Content-Type: application/json; charset=utf-8');
51
        http_response_code($code);
52
        die(json_encode(['response' => ['status' => $code, 'result' => $data]]));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
53
    }
54
55
    /**
56
     * @param int $code
57
     * @param null|string $message
58
     */
59
    public static function throwError(int $code, ?string $message = null): void
60
    {
61
        header('Content-Type: application/json; charset=utf-8');
62
        http_response_code($code);
63
        die(json_encode([
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
64
            'error' => [
65
                'status' => $code,
66
                'message' => ($message ? $message : 'Nenhum token fornecido'),
67
            ],
68
        ]));
69
    }
70
71
}
72