From e11169f16ae6a28e27c9f0d23ab1c32b43a63d8b Mon Sep 17 00:00:00 2001 From: Morven Lewis-Everley Date: Thu, 14 Nov 2024 18:44:35 +0000 Subject: [PATCH] Move price and customise loops into seperate methods and enusre they run on update --- src/Factory/LineItemFactory.php | 79 ++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/src/Factory/LineItemFactory.php b/src/Factory/LineItemFactory.php index 87f305f..ce9a0e2 100644 --- a/src/Factory/LineItemFactory.php +++ b/src/Factory/LineItemFactory.php @@ -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. @@ -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; } @@ -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