Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
12 / 12 |
CRAP | |
100.00% |
31 / 31 |
Config | |
100.00% |
1 / 1 |
|
100.00% |
12 / 12 |
20 | |
100.00% |
31 / 31 |
__construct | |
100.00% |
1 / 1 |
3 | |
100.00% |
4 / 4 |
|||
__get | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
__set | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
getType | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getPubsub | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
getPubsubKey | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
getOption | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
hasOption | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
toArray | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
getProperty | |
100.00% |
1 / 1 |
3 | |
100.00% |
6 / 6 |
|||
setProperty | |
100.00% |
1 / 1 |
3 | |
100.00% |
5 / 5 |
|||
getDefaultPubsub | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\FileGenerator; |
4 | |
5 | use Illuminate\Support\Str; |
6 | |
7 | class Config |
8 | { |
9 | /** |
10 | * @var string |
11 | */ |
12 | protected string $type; |
13 | |
14 | /** |
15 | * @var array |
16 | */ |
17 | protected array $pubsub; |
18 | |
19 | /** |
20 | * @var string |
21 | */ |
22 | protected string $output_path; |
23 | |
24 | /** |
25 | * @var mixed |
26 | */ |
27 | protected $content; |
28 | |
29 | /** |
30 | * @var array |
31 | */ |
32 | protected array $options = []; |
33 | |
34 | /** |
35 | * @var int |
36 | */ |
37 | protected int $user_id; |
38 | |
39 | /** |
40 | * @var string |
41 | */ |
42 | protected string $service; |
43 | |
44 | /** |
45 | * @param array $data |
46 | */ |
47 | public function __construct(array $data) |
48 | { |
49 | foreach ($data as $key => $value) { |
50 | if (property_exists($this, $key)) { |
51 | $this->setProperty($key, $value); |
52 | } |
53 | } |
54 | } |
55 | |
56 | /** |
57 | * @param string $name |
58 | * @return mixed |
59 | */ |
60 | public function __get(string $name) |
61 | { |
62 | return $this->getProperty($name); |
63 | } |
64 | |
65 | /** |
66 | * @param string $name |
67 | * @param $value |
68 | */ |
69 | public function __set(string $name, $value) |
70 | { |
71 | $this->setProperty($name, $value); |
72 | } |
73 | |
74 | /** |
75 | * @return string|null |
76 | */ |
77 | public function getType(): ?string |
78 | { |
79 | return str::lower($this->type); |
80 | } |
81 | |
82 | /** |
83 | * @return array |
84 | */ |
85 | public function getPubsub(): array |
86 | { |
87 | if (isset($this->pubsub)) { |
88 | return array_merge($this->getDefaultPubsub(), $this->pubsub); |
89 | } |
90 | |
91 | return $this->getDefaultPubsub(); |
92 | } |
93 | |
94 | /** |
95 | * @param $key |
96 | * @return mixed|null |
97 | */ |
98 | public function getPubsubKey($key) |
99 | { |
100 | $pubsub = $this->getPubsub(); |
101 | |
102 | return $pubsub[$key] ?? null; |
103 | } |
104 | |
105 | /** |
106 | * @param string $key |
107 | * @return mixed|null |
108 | */ |
109 | public function getOption(string $key) |
110 | { |
111 | return $this->options[$key] ?? null; |
112 | } |
113 | |
114 | /** |
115 | * @param $key |
116 | * @return bool |
117 | */ |
118 | public function hasOption($key): bool |
119 | { |
120 | return isset($this->options[$key]); |
121 | } |
122 | |
123 | /** |
124 | * @return array |
125 | */ |
126 | public function toArray(): array |
127 | { |
128 | $array = []; |
129 | |
130 | foreach (get_object_vars($this) as $key => $value) { |
131 | $array[$key] = $this->getProperty($key); |
132 | } |
133 | |
134 | return $array; |
135 | } |
136 | |
137 | /** |
138 | * @param string $name |
139 | * @return mixed |
140 | */ |
141 | protected function getProperty(string $name) |
142 | { |
143 | $method = 'get' . ucfirst(Str::camel($name)); |
144 | |
145 | if (method_exists($this, $method)) { |
146 | return $this->$method(); |
147 | } |
148 | |
149 | if (property_exists($this, $name)) { |
150 | return $this->$name ?? null; |
151 | } |
152 | |
153 | return null; |
154 | } |
155 | |
156 | /** |
157 | * @param string $name |
158 | * @param $value |
159 | */ |
160 | protected function setProperty(string $name, $value): void |
161 | { |
162 | $method = 'set' . ucfirst(Str::camel($name)); |
163 | |
164 | if (method_exists($this, $method)) { |
165 | //$this->$method($value); |
166 | } else { |
167 | if (property_exists($this, $name)) { |
168 | $this->$name = $value; |
169 | } |
170 | } |
171 | } |
172 | |
173 | /** |
174 | * @return array |
175 | */ |
176 | protected function getDefaultPubsub(): array |
177 | { |
178 | return [ |
179 | 'channel' => 'default', |
180 | 'message' => [ |
181 | 'action' => 'default' |
182 | ] |
183 | ]; |
184 | } |
185 | } |