Total Complexity | 43 |
Total Lines | 360 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like SubscriptionPlan often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SubscriptionPlan, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class SubscriptionPlan extends Model |
||
15 | { |
||
16 | /** |
||
17 | * @param array $data |
||
18 | * array de dados do Subscription Plan. |
||
19 | * |
||
20 | * + [`'name'`] string. |
||
21 | * + [`'description'`] string. |
||
22 | * + [`'amount'`] float. |
||
23 | * + [`'frequency'`] string. |
||
24 | * + [`'interval'`] int (opcional). |
||
25 | * + [`'cycles'`] float (opcional). |
||
26 | * + [`'callback_url'`] string (opcional). |
||
27 | * + [`'best_day'`] boolean (opcional). |
||
28 | * + [`'pro_rated_charge'`] boolean (opcional). |
||
29 | * + [`'grace_period'`] int (opcional). |
||
30 | * + [`'installments'`] int (opcional). |
||
31 | * + |
||
32 | * + [`'trial'`] array (opcional) dos dados do Trial. |
||
33 | * + [`'trial'`][`'amount'`] float (opcional). |
||
34 | * + [`'trial'`][`'cycles'`] float (opcional). |
||
35 | */ |
||
36 | public function __construct(?array $data = []) |
||
37 | { |
||
38 | parent::__construct($data); |
||
39 | } |
||
40 | |||
41 | protected function schema(SchemaBuilder $schema): Schema |
||
42 | { |
||
43 | $schema->string('name')->nullable(); |
||
44 | $schema->string('description')->nullable(); |
||
45 | $schema->float('amount')->nullable(); |
||
46 | $schema->string('frequency')->nullable(); |
||
47 | $schema->int('interval')->nullable(); |
||
48 | $schema->float('cycles')->nullable(); |
||
49 | $schema->string('callback_url')->nullable(); |
||
50 | $schema->int('installments')->default(1); |
||
51 | $schema->has('trial', Trial::class)->nullable(); |
||
52 | |||
53 | $schema->bool('best_day')->nullable(); |
||
54 | $schema->bool('pro_rated_charge')->nullable(); |
||
55 | $schema->int('grace_period')->nullable(); |
||
56 | |||
57 | return $schema->build(); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Retorna o valor da propriedade name |
||
62 | * |
||
63 | * @return string|null |
||
64 | */ |
||
65 | public function getName(): ?string |
||
66 | { |
||
67 | return $this->get('name'); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Seta o valor da propriedade name |
||
72 | * |
||
73 | * @param string|null $name |
||
74 | * @return self |
||
75 | */ |
||
76 | public function setName(?string $name = null): self |
||
77 | { |
||
78 | $this->set('name', $name); |
||
79 | return $this; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Retorna o valor da propriedade description |
||
84 | * |
||
85 | * @return string|null |
||
86 | */ |
||
87 | public function getDescription(): ?string |
||
88 | { |
||
89 | return $this->get('description'); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Seta o valor da propriedade description |
||
94 | * |
||
95 | * @param string|null $description |
||
96 | * @return self |
||
97 | */ |
||
98 | public function setDescription(?string $description = null): self |
||
99 | { |
||
100 | $this->set('description', $description); |
||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | protected function amount(): Mutator |
||
105 | { |
||
106 | return new Mutator( |
||
107 | null, |
||
108 | fn ($value, $ctx) => |
||
109 | is_null($value) ? $value |
||
110 | : ( |
||
111 | (settype($value, 'float')) && |
||
112 | $value >= 0 ? $value : |
||
113 | $ctx->raise('inválido') |
||
114 | ) |
||
115 | ); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Retorna o valor da propriedade amount |
||
120 | * |
||
121 | * @return float|null |
||
122 | */ |
||
123 | public function getAmount(): ?float |
||
124 | { |
||
125 | return $this->get('amount'); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Seta o valor da propriedade amount |
||
130 | * |
||
131 | * @param float|null $amount |
||
132 | * @return self |
||
133 | */ |
||
134 | public function setAmount(?float $amount = null): self |
||
135 | { |
||
136 | $this->set('amount', $amount); |
||
137 | return $this; |
||
138 | } |
||
139 | |||
140 | protected function frequency(): Mutator |
||
150 | ); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Retorna o valor da propriedade frequency |
||
155 | * |
||
156 | * @return string|null |
||
157 | */ |
||
158 | public function getFrequency(): ?string |
||
159 | { |
||
160 | return $this->get('frequency'); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Seta o valor da propriedade frequency |
||
165 | * |
||
166 | * @param string|null $frequency |
||
167 | * @return self |
||
168 | */ |
||
169 | public function setFrequency(?string $frequency = null): self |
||
170 | { |
||
171 | $this->set('frequency', $frequency); |
||
172 | return $this; |
||
173 | } |
||
174 | |||
175 | protected function interval(): Mutator |
||
176 | { |
||
177 | return new Mutator( |
||
178 | null, |
||
179 | fn ($value, $ctx) => |
||
180 | is_null($value) ? $value : ( |
||
181 | (settype($value, 'int')) && |
||
182 | $value >= 1 && $value <= 12 ? $value : |
||
183 | $ctx->raise('inválido') |
||
184 | ) |
||
185 | ); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Retorna o valor da propriedade interval |
||
190 | * |
||
191 | * @return integer|null |
||
192 | */ |
||
193 | public function getInterval(): ?int |
||
194 | { |
||
195 | return $this->get('interval'); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Seta o valor da propriedade interval |
||
200 | * |
||
201 | * @param integer|null $interval |
||
202 | * @return self |
||
203 | */ |
||
204 | public function setInterval(?int $interval = null): self |
||
205 | { |
||
206 | $this->set('interval', $interval); |
||
207 | return $this; |
||
208 | } |
||
209 | |||
210 | protected function cycles(): Mutator |
||
211 | { |
||
212 | return new Mutator( |
||
213 | null, |
||
214 | fn ($value, $ctx) => |
||
215 | is_null($value) ? $value : ( |
||
216 | floatval($value) >= 0 ? $value : |
||
217 | $ctx->raise('inválido') |
||
218 | ) |
||
219 | ); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Retorna o valor da propriedade cycles |
||
224 | * |
||
225 | * @return integer|null |
||
226 | */ |
||
227 | public function getCycles(): ?int |
||
228 | { |
||
229 | return $this->get('cycles'); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Seta o valor da propriedade cycles |
||
234 | * |
||
235 | * @param integer|null $cycles |
||
236 | * @return self |
||
237 | */ |
||
238 | public function setCycles(?int $cycles = null): self |
||
239 | { |
||
240 | $this->set('cycles', $cycles); |
||
241 | return $this; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Retorna o valor da propriedade callback_url |
||
246 | * |
||
247 | * @return string|null |
||
248 | */ |
||
249 | public function getCallbackUrl(): ?string |
||
250 | { |
||
251 | return $this->get('callback_url'); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Seta o valor da propriedade callback_url |
||
256 | * |
||
257 | * @param string|null $callbackUrl |
||
258 | * @return self |
||
259 | */ |
||
260 | public function setCallbackUrl(?string $callbackUrl = null): self |
||
261 | { |
||
262 | $this->set('callback_url', $callbackUrl); |
||
263 | return $this; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Retorna o objeto da propriedade Trial |
||
268 | * |
||
269 | * @return Trial|null |
||
270 | */ |
||
271 | public function getTrial(): ?Trial |
||
272 | { |
||
273 | return $this->get('trial'); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Seta o objeto da propriedade Trial |
||
278 | * |
||
279 | * @param Trial|null $trial |
||
280 | * @return self |
||
281 | */ |
||
282 | public function setTrial(?Trial $trial = null): self |
||
283 | { |
||
284 | $this->set('trial', $trial); |
||
285 | return $this; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Retorna o valor da propriedade best_day |
||
290 | * |
||
291 | * @return boolean|null |
||
292 | */ |
||
293 | public function getBestDay(): ?bool |
||
294 | { |
||
295 | return $this->get('best_day'); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Seta o valor da propriedade best_day |
||
300 | * |
||
301 | * @param boolean|null $bestDay |
||
302 | * @return self |
||
303 | */ |
||
304 | public function setBestDay(?bool $bestDay = null): self |
||
305 | { |
||
306 | $this->set('best_day', $bestDay); |
||
307 | return $this; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Retorna o valor da propriedade pro_rated_charge |
||
312 | * |
||
313 | * @return boolean|null |
||
314 | */ |
||
315 | public function getProRatedCharge(): ?bool |
||
316 | { |
||
317 | return $this->get('pro_rated_charge'); |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Seta o valor da propriedade pro_rated_charge |
||
322 | * |
||
323 | * @param boolean|null $proRatedCharge |
||
324 | * @return self |
||
325 | */ |
||
326 | public function setProRatedCharge(?bool $proRatedCharge = null): self |
||
327 | { |
||
328 | $this->set('pro_rated_charge', $proRatedCharge); |
||
329 | return $this; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Retorna o valor da propriedade grace_period |
||
334 | * |
||
335 | * @return integer|null |
||
336 | */ |
||
337 | public function getGracePeriod(): ?int |
||
338 | { |
||
339 | return $this->get('grace_period'); |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Seta o valor da propriedade grace_period |
||
344 | * |
||
345 | * @param integer|null $gracePeriod |
||
346 | * @return self |
||
347 | */ |
||
348 | public function setGracePeriod(?int $gracePeriod = null): self |
||
349 | { |
||
350 | $this->set('grace_period', $gracePeriod); |
||
351 | return $this; |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Retorna o valor da propriedade installments |
||
356 | * |
||
357 | * @return integer|null |
||
358 | */ |
||
359 | public function getInstallments(): ?int |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Seta o valor da propriedade installments |
||
366 | * |
||
367 | * @param integer|null $installments |
||
368 | * @return self |
||
369 | */ |
||
370 | public function setInstallments(?int $installments = null): self |
||
371 | { |
||
372 | $this->set('installments', $installments); |
||
373 | return $this; |
||
374 | } |
||
375 | } |
||
376 |