| Total Complexity | 47 |
| Total Lines | 446 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Charge 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 Charge, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | final class Charge extends Model |
||
| 17 | { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param array $data |
||
| 21 | * array de dados do Charge. |
||
| 22 | * |
||
| 23 | * + [`'amount'`] float. |
||
| 24 | * + [`'description'`] string. |
||
| 25 | * + [`'due_date'`] string (opcional) {`Formato: Y-m-d`}. |
||
| 26 | * + [`'frequency'`] int (opcional). |
||
| 27 | * + [`'interval'`] enum {`'day'` | `'week'` | `'month'`} (opcional). |
||
| 28 | * + [`'type'`] enum {`'charge'` | `'recurring'`} (opcional). |
||
| 29 | * + [`'last_charge_date'`] string (opcional). |
||
| 30 | * + [`'callback_url'`] string (opcional). |
||
| 31 | * + [`'auto_debit'`] bool (opcional). |
||
| 32 | * + [`'installments'`] int (opcional). |
||
| 33 | * + [`'is_active'`] bool (opcional). |
||
| 34 | * + [`'products'`] array[`int`, `int`, `int`, ...] (opcional). |
||
| 35 | * + [`'customer'`] array (opcional) dos dados do Customer. |
||
| 36 | * +   [`'name'`] string. |
||
| 37 | * +   [`'email'`] string. |
||
| 38 | * +   [`'phone'`] string. |
||
| 39 | * +   [`'cpf_cnpj'`] string. |
||
| 40 | * + [`'checkout_settings'`] array (opcional) dos dados do CheckoutSettings. |
||
| 41 | * +   [`'max_installments'`] int (opcional). |
||
| 42 | * +   [`'interest_free_installments'`] int (opcional). |
||
| 43 | * +   [`'min_installment_value'`] float (opcional). |
||
| 44 | * +   [`'interest'`] float (opcional). |
||
| 45 | * +   [`'fixed_installment'`] float (opcional). |
||
| 46 | * +   [`'payment_method'`] enum {`'all'` | `'creditcard'` | `'boleto'` | `'transfer'` | `'pix'`} (opcional). |
||
| 47 | * |
||
| 48 | */ |
||
| 49 | public function __construct(?array $data = []) |
||
| 50 | { |
||
| 51 | parent::__construct($data); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function schema(SchemaBuilder $schema): Schema |
||
| 55 | { |
||
| 56 | $schema->float('amount')->nullable(); |
||
| 57 | $schema->string('description')->nullable(); |
||
| 58 | $schema->string('due_date')->nullable(); |
||
| 59 | $schema->int('frequency')->nullable(); |
||
| 60 | $schema->enum('interval', ['day', 'week', 'month'])->nullable(); |
||
| 61 | $schema->enum('type', ['charge', 'recurring'])->nullable(); |
||
| 62 | $schema->string('last_charge_date')->nullable(); |
||
| 63 | $schema->string('callback_url')->nullable(); |
||
| 64 | $schema->bool('auto_debit')->nullable(); |
||
| 65 | $schema->int('installments')->nullable(); |
||
| 66 | $schema->bool('is_active')->nullable(); |
||
| 67 | $schema->any('products')->nullable(); |
||
| 68 | |||
| 69 | $schema->has('customer', Customer::class)->nullable(); |
||
| 70 | |||
| 71 | $schema->has('checkout_settings', CheckoutSettings::class)->nullable(); |
||
| 72 | |||
| 73 | return $schema->build(); |
||
| 74 | } |
||
| 75 | |||
| 76 | protected function amount(): Mutator |
||
| 77 | { |
||
| 78 | return new Mutator( |
||
| 79 | null, |
||
| 80 | fn($value, $ctx) => |
||
| 81 | is_null($value) ? $value : |
||
| 82 | ( |
||
| 83 | Assert::value(floatval($value))->gte(0)->get() |
||
| 84 | ?? $ctx->raise('inválido') |
||
| 85 | ) |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | |||
| 89 | protected function due_date(): Mutator |
||
| 90 | { |
||
| 91 | return new Mutator( |
||
| 92 | null, |
||
| 93 | function ($value, $ctx) { |
||
| 94 | $d = \DateTime::createFromFormat('Y-m-d', $value); |
||
| 95 | |||
| 96 | return is_null($value) || |
||
| 97 | ($d && $d->format('Y-m-d') === $value) ? |
||
| 98 | $value : $ctx->raise('inválido'); |
||
| 99 | } |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | |||
| 103 | protected function frequency(): Mutator |
||
| 104 | { |
||
| 105 | return new Mutator( |
||
| 106 | null, |
||
| 107 | fn($value, $ctx) => |
||
| 108 | is_null($value) ? $value : |
||
| 109 | ( |
||
| 110 | Assert::value(intval($value))->gt(0)->get() |
||
| 111 | ?? $ctx->raise('inválido (informe um valor de 1 à 12)') |
||
| 112 | ) |
||
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | protected function last_charge_date(): Mutator |
||
| 117 | { |
||
| 118 | return new Mutator( |
||
| 119 | null, |
||
| 120 | function ($value, $ctx) { |
||
| 121 | $d = \DateTime::createFromFormat('Y-m-d', $value); |
||
| 122 | |||
| 123 | return is_null($value) || |
||
| 124 | ($d && $d->format('Y-m-d') === $value) ? |
||
| 125 | $value : $ctx->raise('inválido'); |
||
| 126 | } |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | |||
| 130 | protected function installments(): Mutator |
||
| 131 | { |
||
| 132 | return new Mutator( |
||
| 133 | null, |
||
| 134 | fn($value, $ctx) => |
||
| 135 | is_null($value) ? $value : |
||
| 136 | ( |
||
| 137 | Assert::value(intval($value))->gt(0)->get() |
||
| 138 | ?? $ctx->raise('inválido (informe um valor de 1 à 48)') |
||
| 139 | ) |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | protected function products(): Mutator |
||
| 144 | { |
||
| 145 | return new Mutator( |
||
| 146 | null, |
||
| 147 | fn($value, $ctx) => |
||
| 148 | is_null($value) ? $value : |
||
| 149 | ( |
||
| 150 | Assert::value($value)->array() ? $value : |
||
| 151 | $ctx->raise('inválido (informe um array de ids (int) de produtos)') |
||
| 152 | ) |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Retorna o valor da propriedade `amount`. |
||
| 158 | * |
||
| 159 | * @return float|null |
||
| 160 | */ |
||
| 161 | public function getAmount(): ?float |
||
| 162 | { |
||
| 163 | return $this->get('amount'); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Seta o valor da propriedade `amount`. |
||
| 168 | * |
||
| 169 | * @param float|null $amount |
||
| 170 | * @return self |
||
| 171 | */ |
||
| 172 | public function setAmount(?float $amount = null): self |
||
| 173 | { |
||
| 174 | $this->set('amount', $amount); |
||
| 175 | return $this; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Retorna o valor da propriedade `description`. |
||
| 180 | * |
||
| 181 | * @return string|null |
||
| 182 | */ |
||
| 183 | public function getDescription(): ?string |
||
| 184 | { |
||
| 185 | return $this->get('description'); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Seta o valor da propriedade `description`. |
||
| 190 | * |
||
| 191 | * @param string|null $description |
||
| 192 | * @return self |
||
| 193 | */ |
||
| 194 | public function setDescription(?string $description = null): self |
||
| 195 | { |
||
| 196 | $this->set('description', $description); |
||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Retorna o valor da propriedade `due_date`. |
||
| 202 | * |
||
| 203 | * @return string|null |
||
| 204 | */ |
||
| 205 | public function getDueDate(): ?string |
||
| 206 | { |
||
| 207 | return $this->get('due_date'); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Seta o valor da propriedade `due_date`. |
||
| 212 | * |
||
| 213 | * @param string|null $due_date |
||
| 214 | * @return self |
||
| 215 | */ |
||
| 216 | public function setDueDate(?string $due_date = null): self |
||
| 217 | { |
||
| 218 | $this->set('due_date', $due_date); |
||
| 219 | return $this; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Retorna o valor da propriedade `frequency`. |
||
| 224 | * |
||
| 225 | * @return integer|null |
||
| 226 | */ |
||
| 227 | public function getFrequency(): ?int |
||
| 228 | { |
||
| 229 | return $this->get('frequency'); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Seta o valor da propriedade `frequency`. |
||
| 234 | * |
||
| 235 | * @param integer|null $frequency |
||
| 236 | * @return self |
||
| 237 | */ |
||
| 238 | public function setFrequency(?int $frequency = null): self |
||
| 239 | { |
||
| 240 | $this->set('frequency', $frequency); |
||
| 241 | return $this; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Retorna o valor da propriedade `interval`. |
||
| 246 | * |
||
| 247 | * @return string|null |
||
| 248 | */ |
||
| 249 | public function getInterval(): ?string |
||
| 250 | { |
||
| 251 | return $this->get('interval'); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Seta o valor da propriedade `interval`. |
||
| 256 | * |
||
| 257 | * @param string|null $interval |
||
| 258 | * @return self |
||
| 259 | */ |
||
| 260 | public function setInterval(?string $interval = null): self |
||
| 261 | { |
||
| 262 | $this->set('interval', $interval); |
||
| 263 | return $this; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Retorna o valor da propriedade `type`. |
||
| 268 | * |
||
| 269 | * @return string|null |
||
| 270 | */ |
||
| 271 | public function getType(): ?string |
||
| 272 | { |
||
| 273 | return $this->get('type'); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Seta o valor da propriedade `type`. |
||
| 278 | * |
||
| 279 | * @param string|null $type |
||
| 280 | * @return self |
||
| 281 | */ |
||
| 282 | public function setType(?string $type = null): self |
||
| 283 | { |
||
| 284 | $this->set('type', $type); |
||
| 285 | return $this; |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Retorna o valor da propriedade `last_charge_date`. |
||
| 290 | * |
||
| 291 | * @return string|null |
||
| 292 | */ |
||
| 293 | public function getLastChargeDate(): ?string |
||
| 294 | { |
||
| 295 | return $this->get('last_charge_date'); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Seta o valor da propriedade `last_charge_date`. |
||
| 300 | * |
||
| 301 | * @param string|null $last_charge_date |
||
| 302 | * @return self |
||
| 303 | */ |
||
| 304 | public function setLastChargeDate(?string $last_charge_date = null): self |
||
| 305 | { |
||
| 306 | $this->set('last_charge_date', $last_charge_date); |
||
| 307 | return $this; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Retorna o valor da propriedade `callback_url`. |
||
| 312 | * |
||
| 313 | * @return string|null |
||
| 314 | */ |
||
| 315 | public function getCallbackUrl(): ?string |
||
| 316 | { |
||
| 317 | return $this->get('callback_url'); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Seta o valor da propriedade `callback_url`. |
||
| 322 | * |
||
| 323 | * @param string|null $callback_url |
||
| 324 | * @return self |
||
| 325 | */ |
||
| 326 | public function setCallbackUrl(?string $callback_url = null): self |
||
| 327 | { |
||
| 328 | $this->set('callback_url', $callback_url); |
||
| 329 | return $this; |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Retorna o valor da propriedade `auto_debit`. |
||
| 334 | * |
||
| 335 | * @return boolean|null |
||
| 336 | */ |
||
| 337 | public function getAutoDebit(): ?bool |
||
| 338 | { |
||
| 339 | return $this->get('auto_debit'); |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Seta o valor da propriedade `auto_debit`. |
||
| 344 | * |
||
| 345 | * @param boolean|null $auto_debit |
||
| 346 | * @return self |
||
| 347 | */ |
||
| 348 | public function setAutoDebit(?bool $auto_debit = null): self |
||
| 349 | { |
||
| 350 | $this->set('auto_debit', $auto_debit); |
||
| 351 | return $this; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Retorna o valor da propriedade `installments`. |
||
| 356 | * |
||
| 357 | * @return integer|null |
||
| 358 | */ |
||
| 359 | public function getInstallments(): ?int |
||
| 360 | { |
||
| 361 | return $this->get('installments'); |
||
| 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 | /** |
||
| 377 | * Retorna o valor da propriedade `is_active`. |
||
| 378 | * |
||
| 379 | * @return boolean|null |
||
| 380 | */ |
||
| 381 | public function getIsActive(): ?bool |
||
| 382 | { |
||
| 383 | return $this->get('is_active'); |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Seta o valor da propriedade `is_active`. |
||
| 388 | * |
||
| 389 | * @param boolean|null $is_active |
||
| 390 | * @return self |
||
| 391 | */ |
||
| 392 | public function setIsActive(?bool $is_active = null): self |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Retorna o array `Products_Ids` associado ao `Charge`. |
||
| 400 | * |
||
| 401 | * @return array|null |
||
| 402 | */ |
||
| 403 | public function getProducts(): ?array |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Seta o array `Products_Ids` associado ao `Charge`. |
||
| 410 | * |
||
| 411 | * @param array|null $products |
||
| 412 | * @return self |
||
| 413 | */ |
||
| 414 | public function setProducts(?array $products = null): self |
||
| 415 | { |
||
| 416 | $this->set('products', $products); |
||
| 417 | return $this; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Retorna o objeto `Customer` associado ao `Charge`. |
||
| 422 | * |
||
| 423 | * @return Customer|null |
||
| 424 | */ |
||
| 425 | public function getCustomer(): ?Customer |
||
| 426 | { |
||
| 427 | return $this->get('customer'); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Seta o objeto `Customer` associado ao `Charge`. |
||
| 432 | * |
||
| 433 | * @param Customer|null $customer |
||
| 434 | * @return self |
||
| 435 | */ |
||
| 436 | public function setCustomer(?Customer $customer = null): self |
||
| 437 | { |
||
| 438 | $this->set('customer', $customer); |
||
| 439 | return $this; |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Retorna o objeto `CheckoutSettings` associado ao `Charge`. |
||
| 444 | * |
||
| 445 | * @return CheckoutSettings|null |
||
| 446 | */ |
||
| 447 | public function getCheckoutSettings(): ?CheckoutSettings |
||
| 448 | { |
||
| 449 | return $this->get('checkout_settings'); |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Seta o objeto `CheckoutSettings` associado ao `Charge`. |
||
| 454 | * |
||
| 455 | * @param CheckoutSettings|null $checkout_settings |
||
| 456 | * @return self |
||
| 457 | */ |
||
| 458 | public function setCheckoutSettings(?CheckoutSettings $checkout_settings = null): self |
||
| 462 | } |
||
| 463 | |||
| 464 | } |