Skip to content

Commit

Permalink
Move price and customise loops into seperate methods and enusre they …
Browse files Browse the repository at this point in the history
…run on update
  • Loading branch information
mlewis-everley committed Nov 14, 2024
1 parent 8c96c76 commit e11169f
Showing 1 changed file with 48 additions and 31 deletions.
79 changes: 48 additions & 31 deletions src/Factory/LineItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,34 @@ protected function getPotentialCustomisers()
return ClassInfo::implementorsOf(LineItemCustomisable::class);
}

protected function performPriceModifications()
{
$data = $this->getExtraData();
$modifiers = $this->getPotentialPriceModifiers();

foreach ($modifiers as $modifier_class) {
/** @var LineItemPricable */
$pricable = Injector::inst()->get($modifier_class, true);
$pricable->modifyItemPrice($this, $data);
}

return;
}

protected function performCustomisation()
{
$data = $this->getExtraData();
$customisers = $this->getPotentialCustomisers();

foreach ($customisers as $custom_class) {
/** @var LineItemCustomiser */
$customiser = Injector::inst()->get($custom_class, true);
$customiser->customiseLineItem($this, $data);
}

return;
}

/**
* Either find an existing line item (based on the submitted
* data), or generate a new one.
Expand All @@ -156,29 +184,33 @@ protected function getPotentialCustomisers()
public function makeItem(): self
{
$class = self::ITEM_CLASS;

// Setup initial line item
$item = $class::create($this->getItemArray());
$data = $this->getExtraData();

// Setup Key
$item->Key = $this->generateKey();

$this->setItem($item);

$modifiers = $this->getPotentialPriceModifiers();
$customisers = $this->getPotentialCustomisers();
$this->performPriceModifications();
$this->performCustomisation();

foreach ($modifiers as $modifier_class) {
/** @var LineItemPricable */
$pricable = Injector::inst()->get($modifier_class, true);
$pricable->modifyItemPrice($this, $data);
}
return $this;
}

foreach ($customisers as $custom_class) {
/** @var LineItemCustomiser */
$customiser = Injector::inst()->get($custom_class, true);
$customiser->customiseLineItem($this, $data);
}
/**
* Update the current line item
*
* @return self
*/
public function update()
{
$item = $this->getItem();
$item->update($this->getItemArray());
$item->Key = $this->generateKey();

$this->setItem($item);

$this->performPriceModifications();
$this->performCustomisation();

return $this;
}
Expand Down Expand Up @@ -294,21 +326,6 @@ public function modifyPrice(
return $modifier;
}

/**
* Update the current line item
*
* @return self
*/
public function update()
{
$item = $this->getItem();
$item->update($this->getItemArray());
$item->Key = $this->generateKey();
$this->setItem($item);

return $this;
}

/**
* Find the best possible tax rate for a line item. If the item is
* linked to an invoice/estimate, then see if there is a Country
Expand Down

0 comments on commit e11169f

Please sign in to comment.