Skip to content

Instantly share code, notes, and snippets.

@felixcarpena
Created October 18, 2016 07:52
Show Gist options
  • Save felixcarpena/55f18ef9a17821fd21c4d96917600541 to your computer and use it in GitHub Desktop.
Save felixcarpena/55f18ef9a17821fd21c4d96917600541 to your computer and use it in GitHub Desktop.
gilded rose - php - step before polymorphism
<?php
namespace GildedRose;
class GildedRose
{
private $items;
function __construct($items)
{
$this->items = $items;
}
function updateQuality()
{
foreach ($this->items as $item) {
$this->updateItem($item);
}
}
/**
* @param $item
*/
private function updateItem($item)
{
if ($item->name == 'Sulfuras, Hand of Ragnaros') {
return;
}
$item->sell_in = $item->sell_in - 1;
switch ($item->name) {
case 'Aged Brie' :
if ($item->quality < 50) {
$increase = 1;
if ($item->sell_in < 0) {
$increase++;
}
$item->quality = $item->quality + $increase;
}
break;
case 'Backstage passes to a TAFKAL80ETC concert':
if ($item->quality < 50) {
$increase = 1;
if ($item->sell_in < 10) {
$increase++;
}
if ($item->sell_in < 5) {
$increase++;
}
$item->quality = $item->quality + $increase;
}
if ($item->sell_in < 0) {
$item->quality = 0;
}
break;
default:
if ($item->quality > 0) {
$decrease = 1;
if ($item->sell_in < 0) {
$decrease++;
}
$item->quality = $item->quality - $decrease;
}
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment