|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kata\GildedRoseKata; |
|
4
|
|
|
|
|
5
|
|
|
class GildedRose |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
private $items; |
|
9
|
|
|
|
|
10
|
14 |
|
function __construct($items) |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
14 |
|
$this->items = $items; |
|
13
|
14 |
|
} |
|
14
|
|
|
|
|
15
|
14 |
|
function update_quality() |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
14 |
|
foreach ($this->items as $item) |
|
18
|
|
|
{ |
|
19
|
14 |
|
$new_item = $this->factory_method($item); |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
14 |
|
if ($item->name != 'Aged Brie' and $item->name != 'Backstage passes to a TAFKAL80ETC concert') |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
7 |
|
$this->decrementQualityOneUnitIfItemNameIsNotSulfuras($item); |
|
24
|
|
|
} |
|
25
|
|
|
else |
|
26
|
|
|
{ |
|
27
|
7 |
|
$this->incrementQuality($item); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if ($item->name != 'Sulfuras, Hand of Ragnaros') |
|
31
|
|
|
{ |
|
32
|
|
|
$item->sell_in = $item->sell_in - 1; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if ($item->sell_in > 0) |
|
36
|
|
|
{ |
|
37
|
|
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if ($item->name == 'Aged Brie') |
|
41
|
|
|
{ |
|
42
|
|
|
$this->increaseQualityByOneUnitWhenQualitySmallerThanFifty($item); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if ($item->name != 'Backstage passes to a TAFKAL80ETC concert') |
|
46
|
|
|
{ |
|
47
|
|
|
$this->decrementQualityOneUnitIfItemNameIsNotSulfuras($item); |
|
48
|
|
|
} |
|
49
|
|
|
else |
|
50
|
|
|
{ |
|
51
|
|
|
$item->quality = $item->quality - $item->quality; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
3 |
|
public function increaseQualityByOneUnitWhenQualitySmallerThanFifty($item): void |
|
57
|
|
|
{ |
|
58
|
3 |
|
if ($item->quality < 50) |
|
59
|
|
|
{ |
|
60
|
3 |
|
$item->quality = $item->quality + 1; |
|
61
|
|
|
} |
|
62
|
3 |
|
} |
|
63
|
|
|
|
|
64
|
7 |
|
public function decrementQualityOneUnitIfItemNameIsNotSulfuras($item): void |
|
65
|
|
|
{ |
|
66
|
7 |
|
if ($item->quality > 0) |
|
67
|
|
|
{ |
|
68
|
5 |
|
if ($item->name != 'Sulfuras, Hand of Ragnaros') |
|
69
|
|
|
{ |
|
70
|
3 |
|
$item->quality = $item->quality - 1; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
7 |
|
} |
|
74
|
|
|
|
|
75
|
7 |
|
public function incrementQuality($item): void |
|
76
|
|
|
{ |
|
77
|
7 |
|
if ($item->quality >= 50) |
|
78
|
|
|
{ |
|
79
|
1 |
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
6 |
|
$item->quality = $item->quality + 1; |
|
83
|
|
|
|
|
84
|
6 |
|
if ($item->name !== 'Backstage passes to a TAFKAL80ETC concert') |
|
85
|
|
|
{ |
|
86
|
2 |
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
$this->incrementQualityForBackstagePasses($item); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
4 |
|
private function incrementQualityForBackstagePasses($item): void |
|
93
|
|
|
{ |
|
94
|
4 |
|
if ($item->sell_in < 11) |
|
95
|
|
|
{ |
|
96
|
3 |
|
$this->increaseQualityByOneUnitWhenQualitySmallerThanFifty($item); |
|
97
|
|
|
} |
|
98
|
1 |
|
if ($item->sell_in < 6) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->increaseQualityByOneUnitWhenQualitySmallerThanFifty($item); |
|
101
|
|
|
} |
|
102
|
1 |
|
} |
|
103
|
|
|
|
|
104
|
14 |
|
private function factory_method($item) |
|
105
|
|
|
{ |
|
106
|
14 |
|
if ($item->name == BackstagePassesItem::NAME) |
|
107
|
|
|
{ |
|
108
|
4 |
|
$new_item = BackstagePassesItem::createBackstagePasses($item); |
|
109
|
|
|
} |
|
110
|
10 |
|
elseif ($item->name == AgedBrieItem::NAME) |
|
111
|
|
|
{ |
|
112
|
3 |
|
$new_item = AgedBrieItem::createAgedBrieItem($item); |
|
113
|
|
|
} |
|
114
|
7 |
|
elseif ($item->name == SulfurasItem::NAME) |
|
115
|
|
|
{ |
|
116
|
2 |
|
$new_item = SulfurasItem::createSulfurasItem($item); |
|
117
|
|
|
}else{ |
|
118
|
5 |
|
$new_item = $item; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
14 |
|
return $new_item; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.