| Total Complexity | 42 |
| Total Lines | 224 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Payment 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 Payment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Payment implements PaymentInterface |
||
| 31 | { |
||
| 32 | private $id; |
||
| 33 | |||
| 34 | private string $paymentReference; |
||
| 35 | |||
| 36 | private string $provider; |
||
| 37 | |||
| 38 | private PaymentStatus $status; |
||
| 39 | |||
| 40 | private int $amount; |
||
| 41 | |||
| 42 | private string $currency; |
||
| 43 | |||
| 44 | private ?string $description = null; |
||
| 45 | |||
| 46 | private ?CustomerInterface $customer = null; |
||
| 47 | |||
| 48 | private \DateTimeInterface $createdAt; |
||
| 49 | |||
| 50 | private \DateTimeInterface $updatedAt; |
||
| 51 | |||
| 52 | private bool $refunded = false; |
||
| 53 | |||
| 54 | private bool $completed = false; |
||
| 55 | |||
| 56 | private bool $chargedBack = false; |
||
| 57 | |||
| 58 | private Collection $subscriptions; |
||
| 59 | |||
| 60 | private ?string $paymentProviderDetailsUrl; |
||
| 61 | |||
| 62 | public function __construct() |
||
| 63 | { |
||
| 64 | $this->subscriptions = new ArrayCollection(); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function getId() |
||
| 68 | { |
||
| 69 | return $this->id; |
||
| 70 | } |
||
| 71 | |||
| 72 | public function setId($id): void |
||
| 73 | { |
||
| 74 | $this->id = $id; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function getPaymentReference(): string |
||
| 78 | { |
||
| 79 | return $this->paymentReference; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function setPaymentReference(string $paymentReference): void |
||
| 83 | { |
||
| 84 | $this->paymentReference = $paymentReference; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function getProvider(): string |
||
| 88 | { |
||
| 89 | return $this->provider; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function setProvider(string $provider): void |
||
| 93 | { |
||
| 94 | $this->provider = $provider; |
||
| 95 | } |
||
| 96 | |||
| 97 | public function getAmount(): int |
||
| 98 | { |
||
| 99 | return $this->amount; |
||
| 100 | } |
||
| 101 | |||
| 102 | public function setAmount(int $amount): void |
||
| 103 | { |
||
| 104 | $this->amount = $amount; |
||
| 105 | } |
||
| 106 | |||
| 107 | public function getCurrency(): string |
||
| 108 | { |
||
| 109 | return strtoupper($this->currency); |
||
| 110 | } |
||
| 111 | |||
| 112 | public function setCurrency(string $currency): void |
||
| 113 | { |
||
| 114 | $this->currency = strtoupper($currency); |
||
| 115 | } |
||
| 116 | |||
| 117 | public function getCreatedAt(): \DateTimeInterface |
||
| 118 | { |
||
| 119 | return $this->createdAt; |
||
| 120 | } |
||
| 121 | |||
| 122 | public function setCreatedAt(\DateTimeInterface $createdAt): void |
||
| 123 | { |
||
| 124 | $this->createdAt = $createdAt; |
||
| 125 | } |
||
| 126 | |||
| 127 | public function isRefunded(): bool |
||
| 128 | { |
||
| 129 | return $this->refunded; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function setRefunded(bool $refunded): void |
||
| 133 | { |
||
| 134 | $this->refunded = $refunded; |
||
| 135 | } |
||
| 136 | |||
| 137 | public function isCompleted(): bool |
||
| 140 | } |
||
| 141 | |||
| 142 | public function setCompleted(bool $completed): void |
||
| 143 | { |
||
| 144 | $this->completed = $completed; |
||
| 145 | } |
||
| 146 | |||
| 147 | public function isChargedBack(): bool |
||
| 148 | { |
||
| 149 | return $this->chargedBack; |
||
| 150 | } |
||
| 151 | |||
| 152 | public function setChargedBack(bool $chargedBack): void |
||
| 153 | { |
||
| 154 | $this->chargedBack = $chargedBack; |
||
| 155 | } |
||
| 156 | |||
| 157 | public function getUpdatedAt(): \DateTimeInterface |
||
| 158 | { |
||
| 159 | return $this->updatedAt; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function setUpdatedAt(\DateTimeInterface $updatedAt): void |
||
| 163 | { |
||
| 164 | $this->updatedAt = $updatedAt; |
||
| 165 | } |
||
| 166 | |||
| 167 | public function getCustomer(): ?CustomerInterface |
||
| 170 | } |
||
| 171 | |||
| 172 | public function setCustomer(CustomerInterface $customer): void |
||
| 173 | { |
||
| 174 | $this->customer = $customer; |
||
| 175 | } |
||
| 176 | |||
| 177 | public function hasCustomer(): bool |
||
| 178 | { |
||
| 179 | return isset($this->customer); |
||
| 180 | } |
||
| 181 | |||
| 182 | public function getMoneyAmount(): Money |
||
| 185 | } |
||
| 186 | |||
| 187 | public function setMoneyAmount(Money $money) |
||
| 188 | { |
||
| 189 | $this->amount = $money->getMinorAmount()->toInt(); |
||
| 190 | $this->currency = $money->getCurrency()->getCurrencyCode(); |
||
| 191 | } |
||
| 192 | |||
| 193 | public function getSubscription(): ?Subscription |
||
| 194 | { |
||
| 195 | return $this->subscription; |
||
| 196 | } |
||
| 197 | |||
| 198 | public function setSubscription(?Subscription $subscription): void |
||
| 199 | { |
||
| 200 | $this->subscription = $subscription; |
||
|
|
|||
| 201 | } |
||
| 202 | |||
| 203 | public function getPaymentProviderDetailsUrl(): ?string |
||
| 204 | { |
||
| 205 | return $this->paymentProviderDetailsUrl; |
||
| 206 | } |
||
| 207 | |||
| 208 | public function setPaymentProviderDetailsUrl(?string $paymentProviderDetailsUrl): void |
||
| 209 | { |
||
| 210 | $this->paymentProviderDetailsUrl = $paymentProviderDetailsUrl; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function getSubscriptions(): Collection |
||
| 214 | { |
||
| 215 | return $this->subscriptions; |
||
| 216 | } |
||
| 217 | |||
| 218 | public function setSubscriptions(Collection|array $subscriptions): void |
||
| 221 | } |
||
| 222 | |||
| 223 | public function addSubscription(Subscription $subscription): void |
||
| 224 | { |
||
| 225 | if ($this->subscriptions instanceof Collection && !$this->subscriptions->contains($subscription)) { |
||
| 226 | $this->subscriptions->add($subscription); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | public function getStatus(): PaymentStatus |
||
| 231 | { |
||
| 232 | return $this->status; |
||
| 233 | } |
||
| 234 | |||
| 235 | public function setStatus(PaymentStatus $status): void |
||
| 236 | { |
||
| 237 | if (PaymentStatus::PARTIALLY_REFUNDED === $status || PaymentStatus::FULLY_REFUNDED === $status) { |
||
| 238 | $this->refunded = true; |
||
| 239 | } elseif (PaymentStatus::DISPUTED === $status) { |
||
| 240 | $this->chargedBack = true; |
||
| 241 | } |
||
| 242 | |||
| 243 | $this->status = $status; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function getDescription(): ?string |
||
| 249 | } |
||
| 250 | |||
| 251 | public function setDescription(string $description): void |
||
| 252 | { |
||
| 253 | $this->description = $description; |
||
| 254 | } |
||
| 255 | } |
||
| 256 |