From d32604ed1ab794882433e89da25d52656e38d316 Mon Sep 17 00:00:00 2001 From: Ufuk Sakar Date: Fri, 2 Aug 2024 16:07:25 +0300 Subject: [PATCH] feat: conjured implementation --- php/src/GildedRose.php | 137 +++++++++++++++++++++++++++-------------- 1 file changed, 91 insertions(+), 46 deletions(-) diff --git a/php/src/GildedRose.php b/php/src/GildedRose.php index db81ee3b..9da058fc 100644 --- a/php/src/GildedRose.php +++ b/php/src/GildedRose.php @@ -11,57 +11,102 @@ final class GildedRose */ public function __construct( private array $items - ) { + ) + { } public function updateQuality(): void { foreach ($this->items as $item) { - if ($item->name != 'Aged Brie' and $item->name != 'Backstage passes to a TAFKAL80ETC concert') { - if ($item->quality > 0) { - if ($item->name != 'Sulfuras, Hand of Ragnaros') { - $item->quality = $item->quality - 1; - } - } - } else { - if ($item->quality < 50) { - $item->quality = $item->quality + 1; - if ($item->name == 'Backstage passes to a TAFKAL80ETC concert') { - if ($item->sellIn < 11) { - if ($item->quality < 50) { - $item->quality = $item->quality + 1; - } - } - if ($item->sellIn < 6) { - if ($item->quality < 50) { - $item->quality = $item->quality + 1; - } - } - } - } - } - - if ($item->name != 'Sulfuras, Hand of Ragnaros') { - $item->sellIn = $item->sellIn - 1; - } - - if ($item->sellIn < 0) { - if ($item->name != 'Aged Brie') { - if ($item->name != 'Backstage passes to a TAFKAL80ETC concert') { - if ($item->quality > 0) { - if ($item->name != 'Sulfuras, Hand of Ragnaros') { - $item->quality = $item->quality - 1; - } - } - } else { - $item->quality = $item->quality - $item->quality; - } - } else { - if ($item->quality < 50) { - $item->quality = $item->quality + 1; - } - } - } + $this->updateItemQuality($item); } } + + private function updateItemQuality($item): void + { + if ($item->name == 'Sulfuras, Hand of Ragnaros') { + return; // Legendary item, no changes needed + } + + $this->decreaseSellIn($item); + + switch ($item->name) { + case 'Aged Brie': + $this->updateAgedBrie($item); + break; + case 'Backstage passes to a TAFKAL80ETC concert': + $this->updateBackstagePasses($item); + break; + case 'Conjured Mana Cake': + $this->updateConjured($item); + break; + default: + $this->updateNormalItem($item); + } + + $this->ensureQualityInRange($item); + } + private function decreaseSellIn($item): void + { + $item->sellIn--; + } + + private function updateAgedBrie($item): void + { + $this->increaseQuality($item); + if ($item->sellIn < 0) { + $this->increaseQuality($item); + } + } + + private function updateBackstagePasses($item): void + { + $this->increaseQuality($item); + if ($item->sellIn < 10) { + $this->increaseQuality($item); + } + if ($item->sellIn < 5) { + $this->increaseQuality($item); + } + if ($item->sellIn < 0) { + $item->quality = 0; + } + } + + private function updateConjured($item): void + { + $this->decreaseQuality($item); + $this->decreaseQuality($item); + if ($item->sellIn < 0) { + $this->decreaseQuality($item); + $this->decreaseQuality($item); + } + } + + private function updateNormalItem($item): void + { + $this->decreaseQuality($item); + if ($item->sellIn < 0) { + $this->decreaseQuality($item); + } + } + + private function increaseQuality($item): void + { + if ($item->quality < 50) { + $item->quality++; + } + } + + private function decreaseQuality($item): void + { + if ($item->quality > 0) { + $item->quality--; + } + } + + private function ensureQualityInRange($item): void + { + $item->quality = max(0, min(50, $item->quality)); + } }