From b6b41e3d94c06d9650a0a90898290ff67b90471b Mon Sep 17 00:00:00 2001 From: Kacper Majczak Date: Mon, 12 Sep 2022 14:23:40 +0200 Subject: [PATCH] Move body from client to command classes --- php/src/GildedRose.php | 82 +++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 25 deletions(-) diff --git a/php/src/GildedRose.php b/php/src/GildedRose.php index bf238b13..233bca2c 100644 --- a/php/src/GildedRose.php +++ b/php/src/GildedRose.php @@ -22,38 +22,70 @@ final class GildedRose public function updateQuality(): void { foreach ($this->items as $item) { - if ($item->name !== self::SULFURAS_HAND_OF_RAGNAROS) { - $item->decreaseSellDate(); - } - if ($item->name === self::AGED_BRIE) { - $item->increaseQuality(); - if ($item->isSellDatePassed()) { - $item->increaseQuality(); - } + (new AgedBrieCommand())->execute($item); } if ($item->name === self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) { - $item->increaseQuality(); - if ($item->sell_in <= 10) { - $item->increaseQuality(); - } - if ($item->sell_in <= 5) { - $item->increaseQuality(); - } + (new BackstageCommand())->execute($item); } - - if (in_array($item->name, [self::AGED_BRIE, self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT, self::SULFURAS_HAND_OF_RAGNAROS]) === false) { - $item->decreaseQuality(); - if ($item->isSellDatePassed()) { - $item->decreaseQuality(); - } - } - - if ($item->isSellDatePassed() && $item->name === self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) { - $item->quality = 0; + if (in_array($item->name, + [ + self::AGED_BRIE, + self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT, + self::SULFURAS_HAND_OF_RAGNAROS + ] + ) === false) { + (new NormalCommand())->execute($item); } } } } + +final class AgedBrieCommand +{ + public function execute(Item $item): void + { + $item->decreaseSellDate(); + + $item->increaseQuality(); + if ($item->isSellDatePassed()) { + $item->increaseQuality(); + } + } +} + +final class BackstageCommand +{ + public function execute(Item $item): void + { + $item->decreaseSellDate(); + + if ($item->isSellDatePassed()) { + $item->quality = 0; + return; + } + + $item->increaseQuality(); + if ($item->sell_in <= 10) { + $item->increaseQuality(); + } + + if ($item->sell_in <= 5) { + $item->increaseQuality(); + } + } +} + +final class NormalCommand +{ + public function execute(Item $item): void + { + $item->decreaseSellDate(); + $item->decreaseQuality(); + if ($item->isSellDatePassed()) { + $item->decreaseQuality(); + } + } +} \ No newline at end of file