Completed
Push — master ( 76efd2...9ec1c2 )
by Clayton
05:36
created

AbstractPayload::setData()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 9.8333
cc 3
nc 1
nop 1
1
<?php
2
3
namespace BrightComponents\Common\Payloads;
4
5
use Illuminate\Support\Collection;
6
7
abstract class AbstractPayload
8
{
9
    /**
10
     * The payload data.
11
     *
12
     * @var mixed|null
13
     */
14
    protected $data = null;
15
16
    /**
17
     * The payload status code.
18
     *
19
     * @var int
20
     */
21
    protected $status = 200;
22
23
    /**
24
     * Construct a new Payload class.
25
     *
26
     * @param  mixed|null
27
     */
28
    public function __construct($data, $status = 200)
29
    {
30
        $this->setData($data);
31
        $this->setStatus($status);
32
    }
33
34
    /**
35
     * Set the response data.
36
     *
37
     * @return mixed|null
38
     */
39
    public function setData($data = null)
40
    {
41
        return tap($this, function ($payload) use ($data) {
42
            if (is_array($data)) {
43
                return $payload->data = $data;
44
            }
45
            if ($data instanceof Collection) {
46
                return $payload->data = $data->all();
47
            }
48
49
            return $payload->data = $data;
50
        });
51
    }
52
53
    /**
54
     * Set the response status.
55
     *
56
     * @return $this
57
     */
58
    public function setStatus($status = 200)
59
    {
60
        return tap($this, function ($payload) use ($status) {
61
            return $payload->status = $status;
62
        });
63
    }
64
65
    /**
66
     * Get the payload data.
67
     *
68
     * @return mixed|null
69
     */
70
    public function getData()
71
    {
72
        return $this->data;
73
    }
74
75
    /**
76
     * Get the payload status.
77
     *
78
     * @return int
79
     */
80
    public function getStatus()
81
    {
82
        return $this->status;
83
    }
84
}
85