Skip to content

Instantly share code, notes, and snippets.

@gquemener
Created July 27, 2023 15:42
Show Gist options
  • Save gquemener/cbdfc81974049be5888ae93c37555e89 to your computer and use it in GitHub Desktop.
Save gquemener/cbdfc81974049be5888ae93c37555e89 to your computer and use it in GitHub Desktop.
<?php
interface Bonus
{
public function apply(Amount $amount): Amount;
}
class AbsoluteBonus implements Bonus
{
public static function fromInt(int $value): self
{
}
public function apply(Amount $amount): Amount
{
return $amount->sub($this->value);
}
}
class RelativeBonus implements Bonus
{
public static function fromInt(int $value): self
{
}
public function apply(Amount $amount): Amount
{
return $amount->multiply($this->value / 100);
}
}
class PaymentMode
{
private ?Bonus $bonus;
public function applyBonus(Amount $amount): Amount
{
if (null === $this->bonus) {
return $amount;
}
return $this->bonus->apply($amount);
}
}
class Order
{
private PaymentMode $paymentMode;
public function total(): Amount
{
return $this->paymentMode->applyBonus($this->getItemsTotal());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment