Passed
Push — master ( c57d84...4857dc )
by Songda
02:27 queued 27s
created

Rocket::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 17
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 21
rs 9.7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yansongda\Pay;
6
7
use ArrayAccess;
8
use JsonSerializable as JsonSerializableInterface;
9
use Psr\Http\Message\MessageInterface;
10
use Psr\Http\Message\RequestInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Yansongda\Pay\Contract\DirectionInterface;
13
use Yansongda\Pay\Contract\PackerInterface;
14
use Yansongda\Supports\Collection;
15
use Yansongda\Supports\Traits\Accessable;
16
use Yansongda\Supports\Traits\Arrayable;
17
use Yansongda\Supports\Traits\Serializable;
18
19
class Rocket implements JsonSerializableInterface, ArrayAccess
20
{
21
    use Accessable;
22
    use Arrayable;
23
    use Serializable;
24
25
    private ?RequestInterface $radar = null;
26
27
    private array $params = [];
28
29
    private ?Collection $payload = null;
30
31
    private string $packer = PackerInterface::class;
32
33
    private string $direction = DirectionInterface::class;
34
35
    private null|Collection|MessageInterface $destination = null;
36
37
    private null|RequestInterface|ResponseInterface $destinationOrigin = null;
38
39
    public function getRadar(): ?RequestInterface
40
    {
41
        return $this->radar;
42
    }
43
44
    public function setRadar(?RequestInterface $radar): Rocket
45
    {
46
        $this->radar = $radar;
47
48
        return $this;
49
    }
50
51
    public function getParams(): array
52
    {
53
        return $this->params;
54
    }
55
56
    public function setParams(array $params): Rocket
57
    {
58
        $this->params = $params;
59
60
        return $this;
61
    }
62
63
    public function mergeParams(array $params): Rocket
64
    {
65
        $this->params = array_merge($this->params, $params);
66
67
        return $this;
68
    }
69
70
    public function getPayload(): ?Collection
71
    {
72
        return $this->payload;
73
    }
74
75
    public function setPayload(null|array|Collection $payload): Rocket
76
    {
77
        if (is_array($payload)) {
0 ignored issues
show
introduced by
The condition is_array($payload) is always true.
Loading history...
78
            $payload = new Collection($payload);
79
        }
80
81
        $this->payload = $payload;
82
83
        return $this;
84
    }
85
86
    public function mergePayload(array $payload): Rocket
87
    {
88
        if (empty($this->payload)) {
89
            $this->payload = new Collection();
90
        }
91
92
        $this->payload = $this->payload->merge($payload);
0 ignored issues
show
Bug introduced by
The method merge() does not exist on null. ( Ignorable by Annotation )

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

92
        /** @scrutinizer ignore-call */ 
93
        $this->payload = $this->payload->merge($payload);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
94
        return $this;
95
    }
96
97
    public function exceptPayload(mixed $key): Rocket
98
    {
99
        if (empty($this->payload)) {
100
            return $this;
101
        }
102
103
        $this->payload = $this->payload->except($key);
104
105
        return $this;
106
    }
107
108
    public function getPacker(): string
109
    {
110
        return $this->packer;
111
    }
112
113
    public function setPacker(string $packer): Rocket
114
    {
115
        $this->packer = $packer;
116
117
        return $this;
118
    }
119
120
    public function getDirection(): string
121
    {
122
        return $this->direction;
123
    }
124
125
    public function setDirection(string $direction): Rocket
126
    {
127
        $this->direction = $direction;
128
129
        return $this;
130
    }
131
132
    public function getDestination(): null|Collection|MessageInterface
133
    {
134
        return $this->destination;
135
    }
136
137
    public function setDestination(null|Collection|MessageInterface $destination): Rocket
138
    {
139
        $this->destination = $destination;
140
141
        return $this;
142
    }
143
144
    public function getDestinationOrigin(): null|RequestInterface|ResponseInterface
145
    {
146
        return $this->destinationOrigin;
147
    }
148
149
    public function setDestinationOrigin(null|RequestInterface|ResponseInterface $destinationOrigin): Rocket
150
    {
151
        $this->destinationOrigin = $destinationOrigin;
152
153
        return $this;
154
    }
155
156
    public function toArray(): array
157
    {
158
        $request = $this->getRadar();
159
        $destination = $this->getDestinationOrigin();
160
161
        return [
162
            'radar' => [
163
                'url' => $request?->getUri()->__toString(),
164
                'method' => $request?->getMethod(),
165
                'headers' => $request?->getHeaders(),
166
                'body' => (string) $request?->getBody(),
167
            ],
168
            'params' => $this->getParams(),
169
            'payload' => $this->getPayload()?->toArray(),
170
            'packer' => $this->getPacker(),
171
            'direction' => $this->getDirection(),
172
            'destination' => $this->getDestination(),
173
            'destination_origin' => [
174
                'status' => $destination instanceof ResponseInterface ? $destination->getStatusCode() : null,
175
                'headers' => $destination?->getHeaders(),
176
                'body' => (string) $destination?->getBody(),
177
            ],
178
        ];
179
    }
180
}
181