Success   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 14
c 1
b 0
f 0
dl 0
loc 43
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A success() 0 7 1
A noContent() 0 9 1
A toArray() 0 5 1
1
<?php
2
3
/**
4
 * Rest Response
5
 *
6
 * @link        https://github.com/MelquesPaiva/rest-response for the canonical source repository
7
 * @copyright   Copyright (c) MelquesPaiva
8
 */
9
10
namespace MelquesPaiva\RestResponse\HttpResponse;
11
12
/**
13
 * Class Success
14
 * @package MelquesPaiva\RestResponse\HttpResponse
15
 */
16
class Success extends HttpResponse
17
{
18
    /**
19
     * The request has succeeded
20
     * 
21
     * @param string $message
22
     * @param array|null $data
23
     * @return Success
24
     */
25
    public function success(string $message, ?array $data = null): Success
26
    {
27
        $this->statusCode = 200;
28
        $this->message = $message;
29
        $this->data = $data;
30
31
        return $this;
32
    }
33
34
    /**
35
     * The server does not need to return an entity body
36
     *
37
     * @return Success
38
     */
39
    public function noContent(): Success
40
    {
41
        $this->statusCode = 204;
42
        $this->message = "";
43
        $this->data = [];
44
        $this->type = "";
45
        $this->parameter = "";
46
47
        return $this;
48
    }
49
50
    /**
51
     * @param string $nameDataParam
52
     * @return array
53
     */
54
    public function toArray(string $nameDataParam = "data"): array
55
    {
56
        return [
57
            "message" => $this->message,
58
            $nameDataParam => $this->data,
59
        ];
60
    }
61
}
62