Skip to content

Instantly share code, notes, and snippets.

@mikemix
Created August 9, 2022 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikemix/6dd614c096ecf23aaef307609e95b006 to your computer and use it in GitHub Desktop.
Save mikemix/6dd614c096ecf23aaef307609e95b006 to your computer and use it in GitHub Desktop.
<?php
class DiscountApplier
{
private DiscountCalculator $calculator;
public function __construct(DiscountCalculator $calculator)
{
$this->calculator = $calculator;
}
public function apply(Basket $basket): void
{
if ($basket->hasItems()) {
$discount = 0.0;
foreach ($basket->getItems() as $basketItem) {
if ($basketItem->isTaxable()) {
if ($basketItem->getPrice() > 0) {
$discount += $this->calculator->getDiscount($basketItem);
}
}
}
$basket->applyDiscount($discount);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment