ApiResponseHandler   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 108
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 9 1
A exception() 0 7 1
A authenticationError() 0 8 1
A validationError() 0 18 3
A success() 0 8 1
A failure() 0 7 2
1
<?php
2
3
namespace bSecure\Payments\Helpers;
4
5
use Illuminate\Http\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
class ApiResponseHandler
8
{
9
10
    /**
11
     * @param array $body
12
     * @param string $message
13
     * @return JsonResponse
14
     */
15
    public static function success($body = [], $message = "messages.general.success")
16
    {
17
18
        return self::send(
19
            Http::$Codes[Http::SUCCESS],
20
            [Language::getMessage($message)],
21
            (object)$body,
22
            null
23
        );
24
    }
25
26
    /**
27
     * @param $validationErrors (coudle be array of errors or validator object)
0 ignored issues
show
Documentation Bug introduced by
The doc comment (coudle at position 1 could not be parsed: Expected ')' at position 1, but found 'coudle'.
Loading history...
28
     * @param string $message
29
     * @return JsonResponse
30
     */
31
    public static function validationError($validationErrors, $message = "general.validation")
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
    public static function validationError($validationErrors, /** @scrutinizer ignore-unused */ $message = "general.validation")

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
34
        $errorMessages = [];
35
36
        if (is_array($validationErrors)) {
37
            $errorMessages = array_values($validationErrors);
38
        } else {
39
            foreach ($validationErrors->getMessages() as $key => $errors) {
40
                $errorMessages = array_merge($errorMessages, $errors);
41
            }
42
        }
43
44
        return self::send(
45
            Http::$Codes[Http::INPROCESSABLE],
46
            $errorMessages,
47
            (object)[],
48
            null
49
        );
50
    }
51
52
    /**
53
     * @return JsonResponse
54
     */
55
    public static function authenticationError()
56
    {
57
58
        return self::send(
59
            Http::$Codes[Http::UNAUTHORISED],
60
            [Language::getMessage("general.unauthenticated")],
61
            (object)[],
62
            null
63
        );
64
    }
65
66
    /**
67
     * @param string $message
68
     * @param null $exception
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $exception is correct as it would always require null to be passed?
Loading history...
69
     * @param array $body
70
     * @return JsonResponse
71
     */
72
    public static function failure($message = 'general.error', $exception = null, $body = [])
73
    {
74
        return self::send(
75
            Http::$Codes[Http::BAD_REQUEST],
76
            (gettype($message) == "array") ? $message : [Language::getMessage($message)],
77
            (object)$body,
78
            $exception
79
        );
80
    }
81
82
    /**
83
     * @param $code
84
     * @param $message
85
     * @param $exception
86
     * @return JsonResponse
87
     */
88
89
    public static function exception($code, $message, $exception = null)
90
    {
91
        return self::send(
92
            $code,
93
            [$message],
94
            [],
95
            $exception
96
        );
97
    }
98
99
    /**
100
     * @param $status
101
     * @param $message
102
     * @param $body
103
     * @param $exception
104
     * @return JsonResponse
105
     */
106
    private static function send($status, $message, $body, $exception)
107
    {
108
109
        return response()->json([
0 ignored issues
show
Bug introduced by
The function response was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        return /** @scrutinizer ignore-call */ response()->json([
Loading history...
110
            'status' => $status,
111
            'message' => $message,
112
            'body' => $body,
113
            'exception' => $exception
114
        ], $status, [], JSON_UNESCAPED_UNICODE);
115
    }
116
}
117