secret

Gilded Rose kata

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
<?php
 
require_once 'src/Item.php';
 
class GildedRose {
 
const DEFAULT_QUALITY_CHANGE = -1;
const DEGRADES_TWICE_AS_FAST = 2;
const DEFAULT_CHANGE_FOR_BACKSTAGE_PASS = 1;
const CHANGE_FOR_BACKSTAGE_PASS_WHEN_LESS_THAN_5_DAYS = 3;
const CHANGE_FOR_BACKSTAGE_PASS_WHEN_LESS_THAN_10_DAYS = 2;
const MAXIMUM_QUALITY_VALUE = 50;
const MINIMUM_QUALITY_VALUE = 0;
 
public static function updateQuality($items) {
foreach($items as $item) {
 
$qualityChange = self::getQualityChange($item);
$item->setQuality($item->getQuality() + $qualityChange);
if($item->getQuality() > self::MAXIMUM_QUALITY_VALUE) {
$item->setQuality(self::MAXIMUM_QUALITY_VALUE);
}
if($item->getQuality() < self::MINIMUM_QUALITY_VALUE) {
$item->setQuality(self::MINIMUM_QUALITY_VALUE);
}
 
$sellInChange = -1;
if($item->name == "Sulfuras, Hand of Ragnaros") {
$sellInChange = 0;
}
$item->setSellIn($item->getSellIn() + $sellInChange);
 
}
}
 
private static function getQualityChange($item) {
$change = self::DEFAULT_QUALITY_CHANGE;
$change = self::modifyQualityBasedOnItemType($item,$change);
$change = self::modifyQualityBasedOnSellIn($item,$change);
if($item->name == "Backstage passes to a TAFKAL80ETC concert") {
$change = self::countBackstagePassQuality($item);
}
return $change;
}
private static function modifyQualityBasedOnItemType($item,$change) {
switch ($item->getName()) {
case "Aged Brie":
return abs($change);
case "Sulfuras, Hand of Ragnaros":
return 0;
case "Conjured Mana Cake":
return $change * 2;
default:
return $change;
}
}
 
private static function modifyQualityBasedOnSellIn($item,$change) {
if($item->getSellIn() <= 0) {
return $change * self::DEGRADES_TWICE_AS_FAST;
}
return $change;
}
 
private static function countBackstagePassQuality($item) {
if($item->getSellIn() <= 0) {
return -$item->getQuality();
}
if($item->getSellIn() <= 5) {
return self::CHANGE_FOR_BACKSTAGE_PASS_WHEN_LESS_THAN_5_DAYS;
}
if($item->getSellIn() <= 10) {
return self::CHANGE_FOR_BACKSTAGE_PASS_WHEN_LESS_THAN_10_DAYS;
}
return self::DEFAULT_CHANGE_FOR_BACKSTAGE_PASS;
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.