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
{
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();
}
}
}