Message::getData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace Ghc\Rosetta\Messages;
4
5
use Ghc\Rosetta\Configurable;
6
use Ghc\Rosetta\Pipes\Pipeable;
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Support\Arr;
9
use JsonSerializable;
10
use Serializable;
11
12
abstract class Message implements Arrayable, Serializable, JsonSerializable, Pipeable
13
{
14
    use Configurable;
15
16
    /**
17
     * Message data.
18
     *
19
     * @var mixed
20
     */
21
    protected $data;
22
23
    /**
24
     * Message constructor.
25
     *
26
     * @param null|mixed $data
27
     * @param array      $config
28
     */
29
    public function __construct($data = null, $config = [])
30
    {
31
        $this->setData($data ?: $this->newData());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->newData() targeting Ghc\Rosetta\Messages\Message::newData() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
32
        $this->setConfig($config);
33
34
        $this->boot();
35
    }
36
37
    /**
38
     * Boot Transformer.
39
     */
40
    protected function boot()
41
    {
42
    }
43
44
    /**
45
     * @return null
46
     */
47
    public function newData()
48
    {
49
    }
50
51
    /**
52
     * @return mixed
53
     */
54
    public function getData()
55
    {
56
        return $this->data;
57
    }
58
59
    /**
60
     * @param mixed $data
61
     *
62
     * @return self
63
     */
64
    public function setData($data)
65
    {
66
        $this->data = $data;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function serialize()
75
    {
76
        return serialize([
77
            'data'   => $this->toArray(),
78
            'config' => $this->getConfig(),
79
        ]);
80
    }
81
82
    /**
83
     * @param string $serialized
84
     */
85
    public function unserialize($serialized)
86
    {
87
        $unserialized = unserialize($serialized);
88
        $this->fromArray($unserialized['data']);
89
        $this->setConfig($unserialized['config']);
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function jsonSerialize()
96
    {
97
        return $this->toArray();
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function __toString()
104
    {
105
        return (string) $this->getData();
106
    }
107
108
    /**
109
     * @param array $options
110
     *
111
     * @return \Closure
112
     */
113
    public function pipe($options = [])
114
    {
115
        return function ($inputData) use ($options) {
116
            $fromArray = Arr::get($options, 'fromArray', true);
117
            if ($fromArray) {
118
                $this->fromArray($inputData);
119
            } else {
120
                $this->setData($inputData);
121
            }
122
123
            return $this->toArray();
124
        };
125
    }
126
127
    /**
128
     * @param array $data
129
     *
130
     * @return self
131
     */
132
    abstract public function fromArray($data);
133
}
134