Move classes to separate files

This commit is contained in:
Kacper Majczak 2022-09-12 14:33:25 +02:00
parent d18f1c7e6f
commit e2a1459192
7 changed files with 120 additions and 90 deletions

View File

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace GildedRose;
final class AgedBrieCommand implements Command
{
public function execute(Item $item): void
{
$item->decreaseSellDate();
$item->increaseQuality();
if ($item->isSellDatePassed()) {
$item->increaseQuality();
}
}
}

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace GildedRose;
final class BackstageCommand implements Command
{
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();
}
}
}

10
php/src/Command.php Normal file
View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace GildedRose;
interface Command
{
public function execute(Item $item): void;
}

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace GildedRose;
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();
}
}

View File

@ -28,93 +28,3 @@ final class GildedRose
}
}
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
{
$item->decreaseSellDate();
$item->increaseQuality();
if ($item->isSellDatePassed()) {
$item->increaseQuality();
}
}
}
final class BackstageCommand implements Command
{
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 implements Command
{
public function execute(Item $item): void
{
$item->decreaseSellDate();
$item->decreaseQuality();
if ($item->isSellDatePassed()) {
$item->decreaseQuality();
}
}
}
final class NoopCommand implements Command
{
public function execute(Item $item): void
{
// noop
}
}

13
php/src/NoopCommand.php Normal file
View File

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace GildedRose;
final class NoopCommand implements Command
{
public function execute(Item $item): void
{
// noop
}
}

17
php/src/NormalCommand.php Normal file
View File

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace GildedRose;
final class NormalCommand implements Command
{
public function execute(Item $item): void
{
$item->decreaseSellDate();
$item->decreaseQuality();
if ($item->isSellDatePassed()) {
$item->decreaseQuality();
}
}
}