ResponseTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 11
c 2
b 1
f 0
dl 0
loc 56
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFailResponse() 0 3 1
A getSuccessResponse() 0 3 1
A getStatusResponse() 0 18 2
1
<?php
2
3
namespace Itstructure\MFUploader\traits;
4
5
/**
6
 * Trait ResponseTrait
7
 *
8
 * @package Itstructure\MFUploader\traits
9
 *
10
 * @author Andrey Girnik <[email protected]>
11
 */
12
trait ResponseTrait
13
{
14
    /**
15
     * Returns success response.
16
     *
17
     * @param string     $message
18
     * @param array|null $data
19
     *
20
     * @return array
21
     */
22
    protected function getSuccessResponse(string $message, array $data = null): array
23
    {
24
        return $this->getStatusResponse($message, 'success', $data);
25
    }
26
27
    /**
28
     * Returns fail response.
29
     *
30
     * @param string     $message
31
     * @param array|null $data
32
     *
33
     * @return array
34
     */
35
    protected function getFailResponse(string $message, array $data = null): array
36
    {
37
        return $this->getStatusResponse($message, 'fail', $data);
38
    }
39
40
    /**
41
     * Returns status, message and code.
42
     *
43
     * @param string     $message
44
     * @param string     $status
45
     * @param int        $statusCode
46
     * @param array|null $data
47
     *
48
     * @return array
49
     */
50
    protected function getStatusResponse(
51
        string $message,
52
        string $status,
53
        array $data = null,
54
        int $statusCode = 200): array {
55
56
        if (null === $data) {
57
            $data = (object)[];
58
        }
59
60
        \Yii::$app->response->statusCode = $statusCode;
61
62
        return [
63
            'meta' => [
64
                'status' => $status,
65
                'message' => $message,
66
            ],
67
            'data' => $data,
68
        ];
69
    }
70
}
71