MessagesTrait::messageResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 7
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Jidaikobo\Kontiki\Controllers\FileControllerTraits;
4
5
use Jidaikobo\Kontiki\Utils\MessageUtils;
6
use Psr\Http\Message\ResponseInterface as Response;
7
8
trait MessagesTrait
9
{
10
    protected function getMessages(): array
11
    {
12
        return [
13
            'invalid_request' => __(
14
                'invalid_request',
15
                'Invalid request. Please try again.'
16
            ),
17
            'validation_failed' => __(
18
                'validation_failed',
19
                'Data validation failed. Please check your input.'
20
            ),
21
            'upload_success' => __(
22
                'upload_success',
23
                'The file has been successfully uploaded. Input description and Insert.</a>.'
24
            ),
25
            'upload_error' => __(
26
                'upload_error',
27
                'The file could not be uploaded. Please try again.'
28
            ),
29
            'database_update_failed' => __(
30
                'database_update_failed',
31
                'Failed to update the database. Please try again.'
32
            ),
33
            'file_missing' => __(
34
                'file_missing',
35
                'No file uploaded or the file is corrupted.'
36
            ),
37
            'method_not_allowed' => __(
38
                'method_not_allowed',
39
                'Method not allowed.'
40
            ),
41
            'file_not_found' => __(
42
                'file_not_found',
43
                'File not found.'
44
            ),
45
            'update_success' => __(
46
                'update_success',
47
                'Your file has been successfully uploaded. You can now insert it into your post or add a description in the <a href="#" class="link-primary" id="switchToViewTab">Files List</a>.'
48
            ),
49
            'update_failed' => __(
50
                'update_failed',
51
                'Failed to update the database. Please try again.'
52
            ),
53
            'file_id_required' => __(
54
                'file_id_required',
55
                'File ID is required.'
56
            ),
57
            'file_delete_failed' => __(
58
                'file_delete_failed',
59
                'Failed to delete the file.'
60
            ),
61
            'db_update_failed' => __(
62
                'db_update_failed',
63
                'Failed to update the database.'
64
            ),
65
            'file_delete_success' => __(
66
                'file_delete_success',
67
                'File has been deleted successfully.'
68
            ),
69
            'unexpected_error' => __(
70
                'unexpected_error',
71
                'An unexpected error occurred. Please try again later.'
72
            ),
73
        ];
74
    }
75
76
    protected function messageResponse(
77
        Response $response,
78
        string $message,
79
        int $status
80
    ): Response {
81
        $data = ['message' => $message];
82
        return $this->jsonResponse($response, $data, $status);
0 ignored issues
show
Bug introduced by
It seems like jsonResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

82
        return $this->/** @scrutinizer ignore-call */ jsonResponse($response, $data, $status);
Loading history...
83
    }
84
85
    protected function errorResponse(
86
        Response $response,
87
        string $message,
88
        int $status
89
    ): Response {
90
        return $this->messageResponse(
91
            $response,
92
            MessageUtils::alertHtml($message, 'warning'),
93
            $status
94
        );
95
    }
96
97
    protected function successResponse(
98
        Response $response,
99
        string $message
100
    ): Response {
101
        return $this->messageResponse(
102
            $response,
103
            MessageUtils::alertHtml($message),
104
            200
105
        );
106
    }
107
108
    protected function successResponseHtml(
109
        Response $response,
110
        string $message
111
    ): Response {
112
        return $this->messageResponse(
113
            $response,
114
            MessageUtils::alertHtml($message, 'success', false),
115
            200
116
        );
117
    }
118
}
119