Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
70.00% |
7 / 10 |
CRAP | |
84.44% |
38 / 45 |
MiddlewareData | |
0.00% |
0 / 1 |
|
70.00% |
7 / 10 |
20.36 | |
84.44% |
38 / 45 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
prepare | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
get | |
0.00% |
0 / 1 |
4.04 | |
86.67% |
13 / 15 |
|||
set | |
100.00% |
1 / 1 |
2 | |
100.00% |
7 / 7 |
|||
createId | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
transfert | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
return | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
loop | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
next | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
isPrepared | |
0.00% |
0 / 1 |
5.67 | |
33.33% |
2 / 6 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\CommandsLaravel\Middleware\Core; |
4 | |
5 | use Closure; |
6 | use Exception; |
7 | use stdClass; |
8 | |
9 | final class MiddlewareData |
10 | { |
11 | |
12 | /** |
13 | * The object that holds all stdCLass |
14 | * |
15 | * @var stdClass |
16 | */ |
17 | private $data; |
18 | |
19 | /** |
20 | * An array containing middleware names |
21 | * |
22 | * @var array |
23 | */ |
24 | private $prepared; |
25 | |
26 | /** |
27 | * Return all data |
28 | * |
29 | * @var bool |
30 | */ |
31 | |
32 | private $all = false; |
33 | |
34 | /** |
35 | * Undocumented function |
36 | */ |
37 | public function __construct() |
38 | { |
39 | $this->data = new stdClass; |
40 | } |
41 | |
42 | /** |
43 | * Undocumented function |
44 | * |
45 | * @param string|array $key |
46 | * @return self |
47 | */ |
48 | public function prepare($key): self |
49 | { |
50 | $temp = is_array($key) ? $key : [$key]; |
51 | $this->prepared = collect($temp)->map(function ($class) { |
52 | return $class::name(); |
53 | })->toArray(); |
54 | |
55 | return $this; |
56 | } |
57 | |
58 | /** |
59 | * Undocumented function |
60 | * |
61 | * @param string $name |
62 | * @return stdClass |
63 | */ |
64 | public function get(): stdClass |
65 | { |
66 | return $this->isPrepared(false) |
67 | ->next(function () { |
68 | if (!$this->all) { |
69 | if (count($this->prepared) === 1) { |
70 | $return = $this->return($this->prepared[0]); |
71 | unset($this->prepared); |
72 | return $return; |
73 | } |
74 | |
75 | $return = new stdClass(); |
76 | foreach ($this->prepared as $key => $value) { |
77 | $return->$key = $this->return($value); |
78 | } |
79 | |
80 | unset($this->prepared); |
81 | return $return; |
82 | } |
83 | $this->all = false; |
84 | return $this->data; |
85 | }); |
86 | } |
87 | |
88 | /** |
89 | * Undocumented function |
90 | * |
91 | * @param [type] $data |
92 | * @return stdClass |
93 | */ |
94 | public function set(array $data): stdClass |
95 | { |
96 | return $this->isPrepared() |
97 | ->loop(function ($middleware) use ($data) { |
98 | $currentData = $this->return($middleware); |
99 | foreach ($data as $key => $datum) { |
100 | $currentData->{$key} = $datum; |
101 | } |
102 | }) |
103 | ->get(); |
104 | } |
105 | |
106 | /** |
107 | * Undocumented function |
108 | * |
109 | * @param string $key |
110 | * @return stdClass |
111 | */ |
112 | public function createId($key = 'id'): stdClass |
113 | { |
114 | $uniqId = bin2hex(random_bytes(10)); |
115 | |
116 | return $this->isPrepared()->set([$key => $uniqId]); |
117 | } |
118 | |
119 | /** |
120 | * Undocumented function |
121 | * |
122 | * @param string $from |
123 | * @param array $data |
124 | * @return stdClass |
125 | */ |
126 | public function transfert(string $from, array $data): stdClass |
127 | { |
128 | return $this->isPrepared()->set(array_intersect_key((array) $this->return($from::name()), array_fill_keys($data, ''))); |
129 | } |
130 | |
131 | /** |
132 | * Undocumented function |
133 | * |
134 | * @param [type] $name |
135 | * @return stdClass |
136 | */ |
137 | private function return($name): stdClass |
138 | { |
139 | if (!isset($this->data->{$name})) { |
140 | $this->data->{$name} = new stdClass(); |
141 | } |
142 | return $this->data->{$name}; |
143 | } |
144 | |
145 | /** |
146 | * Undocumented function |
147 | * |
148 | * @param Closure $closure |
149 | * @return self |
150 | */ |
151 | private function loop(Closure $closure): self |
152 | { |
153 | foreach ($this->prepared as $middleware) { |
154 | $closure($middleware); |
155 | } |
156 | |
157 | return $this; |
158 | } |
159 | |
160 | /** |
161 | * Undocumented function |
162 | * |
163 | * @param Closure $closure |
164 | * @return mixed |
165 | */ |
166 | private function next(Closure $closure) |
167 | { |
168 | return $closure(); |
169 | } |
170 | |
171 | /** |
172 | * Undocumented function |
173 | * |
174 | * @param boolean $throw |
175 | * @return self |
176 | */ |
177 | private function isPrepared($throw = true): self |
178 | { |
179 | if (isset($this->prepared)) { |
180 | return $this; |
181 | } |
182 | |
183 | if ($throw) { |
184 | throw new Exception('Keys must be set with the prepare method !'); |
185 | } else { |
186 | $this->all = true; |
187 | } |
188 | } |
189 | } |