From d18f1c7e6fae4100d653c35387daec6e500fd069 Mon Sep 17 00:00:00 2001 From: Kacper Majczak Date: Mon, 12 Sep 2022 14:32:37 +0200 Subject: [PATCH] Implement CommandFactory --- php/src/GildedRose.php | 73 +++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 22 deletions(-) diff --git a/php/src/GildedRose.php b/php/src/GildedRose.php index 233bca2c..a48db7fc 100644 --- a/php/src/GildedRose.php +++ b/php/src/GildedRose.php @@ -6,44 +6,65 @@ namespace GildedRose; final class GildedRose { - private const SULFURAS_HAND_OF_RAGNAROS = 'Sulfuras, Hand of Ragnaros'; - private const BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT = 'Backstage passes to a TAFKAL80ETC concert'; - private const AGED_BRIE = 'Aged Brie'; /** * @var Item[] */ private $items; + private CommandFactory $commandFactory; public function __construct(array $items) { $this->items = $items; + $this->commandFactory = new CommandFactory(); } public function updateQuality(): void { foreach ($this->items as $item) { - if ($item->name === self::AGED_BRIE) { - (new AgedBrieCommand())->execute($item); - } + $command = $this->commandFactory->createFor($item->name); - if ($item->name === self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) { - (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) { - (new NormalCommand())->execute($item); - } + $command->execute($item); } } } -final class AgedBrieCommand + +interface Command +{ + public function execute(Item $item): void; +} + +final class CommandFactory +{ + private const SULFURAS_HAND_OF_RAGNAROS = 'Sulfuras, Hand of Ragnaros'; + private const BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT = 'Backstage passes to a TAFKAL80ETC concert'; + private const AGED_BRIE = 'Aged Brie'; + + public function createFor(string $itemName): Command + { + if ($itemName === self::AGED_BRIE) { + return new AgedBrieCommand(); + } + + if ($itemName === self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) { + return new BackstageCommand(); + } + + if (in_array($itemName, + [ + self::AGED_BRIE, + self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT, + self::SULFURAS_HAND_OF_RAGNAROS + ] + ) === false) { + return new NormalCommand(); + } + + return new NoopCommand(); + } +} + +final class AgedBrieCommand implements Command { public function execute(Item $item): void { @@ -56,7 +77,7 @@ final class AgedBrieCommand } } -final class BackstageCommand +final class BackstageCommand implements Command { public function execute(Item $item): void { @@ -78,7 +99,7 @@ final class BackstageCommand } } -final class NormalCommand +final class NormalCommand implements Command { public function execute(Item $item): void { @@ -88,4 +109,12 @@ final class NormalCommand $item->decreaseQuality(); } } +} + +final class NoopCommand implements Command +{ + public function execute(Item $item): void + { + // noop + } } \ No newline at end of file