Move body from client to command classes

This commit is contained in:
Kacper Majczak 2022-09-12 14:23:40 +02:00
parent 795c150ab5
commit b6b41e3d94

View File

@ -22,38 +22,70 @@ final class GildedRose
public function updateQuality(): void public function updateQuality(): void
{ {
foreach ($this->items as $item) { foreach ($this->items as $item) {
if ($item->name !== self::SULFURAS_HAND_OF_RAGNAROS) { if ($item->name === self::AGED_BRIE) {
$item->decreaseSellDate(); (new AgedBrieCommand())->execute($item);
} }
if ($item->name === self::AGED_BRIE) { 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);
}
}
}
}
final class AgedBrieCommand
{
public function execute(Item $item): void
{
$item->decreaseSellDate();
$item->increaseQuality(); $item->increaseQuality();
if ($item->isSellDatePassed()) { if ($item->isSellDatePassed()) {
$item->increaseQuality(); $item->increaseQuality();
} }
} }
}
final class BackstageCommand
{
public function execute(Item $item): void
{
$item->decreaseSellDate();
if ($item->isSellDatePassed()) {
$item->quality = 0;
return;
}
if ($item->name === self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) {
$item->increaseQuality(); $item->increaseQuality();
if ($item->sell_in <= 10) { if ($item->sell_in <= 10) {
$item->increaseQuality(); $item->increaseQuality();
} }
if ($item->sell_in <= 5) { if ($item->sell_in <= 5) {
$item->increaseQuality(); $item->increaseQuality();
} }
} }
}
final class NormalCommand
if (in_array($item->name, [self::AGED_BRIE, self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT, self::SULFURAS_HAND_OF_RAGNAROS]) === false) { {
public function execute(Item $item): void
{
$item->decreaseSellDate();
$item->decreaseQuality(); $item->decreaseQuality();
if ($item->isSellDatePassed()) { if ($item->isSellDatePassed()) {
$item->decreaseQuality(); $item->decreaseQuality();
} }
} }
if ($item->isSellDatePassed() && $item->name === self::BACKSTAGE_PASSES_TO_A_TAFKAL_80_ETC_CONCERT) {
$item->quality = 0;
}
}
}
} }