-
Notifications
You must be signed in to change notification settings - Fork 4
/
behavioral-memento.php
215 lines (188 loc) · 5.67 KB
/
behavioral-memento.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* This example just to simulate how Memento can be work
* 2nees.com
*/
// Fake Status List
const STATUS = [
"ACTIVE",
"PENDING",
"REJECTED",
"APPROVED",
"RUNNING",
"IN_PROGRESS",
"DELETED",
"HOLD"
];
/**
* Memento Interface which its used to retrive meta data only, keep in you mind you must prevent access state from
** any class except the Originator
*/
interface MementoTicket
{
public function getTicketName(): string;
public function getCreatedAt(): string;
}
/**
* Class TicketOriginator - This is the Originator which its the owner ob state and this class save and restore data
** from Memento
*/
class TicketOriginator
{
private int $ticketNumber;
private string $ticketOwner;
private string $ticketStatus;
/**
* TicketOriginator constructor.
* @param int $ticketNumber
* @param string $ticketOwner
*/
public function __construct(int $ticketNumber, string $ticketOwner)
{
$this->ticketNumber = $ticketNumber;
$this->ticketOwner = $ticketOwner;
$this->ticketStatus = STATUS[rand(0, 7)];
}
/**
* @return string
*/
public function getTicketStatus(): string
{
return $this->ticketStatus;
}
/**
* @param string $ticketStatus
*/
public function setTicketStatus(string $ticketStatus): void
{
$this->ticketStatus = $ticketStatus;
}
/**
* @return int
*/
public function getTicketNumber(): int
{
return $this->ticketNumber;
}
/**
* @param int $ticketNumber
*/
public function setTicketNumber(int $ticketNumber): void
{
$this->ticketNumber = $ticketNumber;
}
/**
* @return string
*/
public function getTicketOwner(): string
{
return $this->ticketOwner;
}
/**
* @param string $ticketOwner
*/
public function setTicketOwner(string $ticketOwner): void
{
$this->ticketOwner = $ticketOwner;
}
public function takeSnapShot(): MementoTicket
{
return new TicketConcreteMemento(clone $this);
}
public function restoreState(MementoTicket $mementoTicket)
{
$state = $mementoTicket->getState();
$this->setTicketNumber($state->getTicketNumber());
$this->setTicketOwner($state->getTicketOwner());
$this->setTicketStatus($state->getTicketStatus());
}
}
/**
* Class TicketConcreteMemento - this class Concrete for memento, implement method and contain infrastructure for store states...
* Important Note: This class must initialize from Constructor, Dont create a setter function!
*/
class TicketConcreteMemento implements MementoTicket
{
private TicketOriginator $state;
private $date;
public function __construct(TicketOriginator $state)
{
$this->state = $state;
$this->date = date('Ymd');
}
public function getState(): TicketOriginator
{
return $this->state;
}
/**
* Some of metadata may used from Originator
*/
public function getTicketName(): string
{
return "memento-state-{$this->date}";
}
public function getCreatedAt(): string
{
return $this->date;
}
}
/**
* Class TicketActions - This is class is Caretaker, and its refer to care about history for this state, so that
** its know about metadata from memento, but this class cant do any action on any state saved in memento,
** so that, this class save state history for originator.
*/
class TicketActions
{
private ?array $statesHistory;
private TicketOriginator $ticketOriginator;
/**
* TicketActions constructor.
* @param TicketOriginator $ticketOriginator
*/
public function __construct(TicketOriginator $ticketOriginator)
{
$this->ticketOriginator = $ticketOriginator;
}
public function backup()
{
$state = $this->ticketOriginator->takeSnapShot();
$this->statesHistory[] = $this->ticketOriginator->takeSnapShot();
$this->printDetails("Backup Saved", $state);
}
public function restore()
{
if(!count($this->statesHistory)){
echo "You cant restore empty data" . PHP_EOL;
return;
}
$state = array_pop($this->statesHistory);
$this->ticketOriginator->restoreState($state);
$this->printDetails("Restored", $state);
}
private function printDetails($action, $state): void
{
echo "{$action}:
#{$state->getCreatedAt()}/{$state->getTicketName()}
- {$this->ticketOriginator->getTicketNumber()}/{$this->ticketOriginator->getTicketOwner()}
- With Status {$this->ticketOriginator->getTicketStatus()}" . PHP_EOL;
}
}
// Client
$ticketOriginator = new TicketOriginator(rand(1, 200), "Anees");
$ticketActions = new TicketActions($ticketOriginator);
$ticketActions->backup();// Save init data
$ticketOriginator->setTicketNumber(rand(200, 400));
$ticketOriginator->setTicketOwner("Hikmat");
$ticketOriginator->setTicketStatus(STATUS[rand(0, 7)]);
$ticketActions->backup();// Save first change
$ticketOriginator->setTicketStatus(STATUS[rand(0, 7)]);
$ticketOriginator->setTicketOwner("2nees.com");
$ticketActions->backup();// Save second change
$ticketOriginator->setTicketNumber(rand(500, 600));// not saved, else if you uncomment bellow...
//$ticketActions->backup(); // Uncomment if you like to save latest random number
echo "Latest Update: " . $ticketOriginator->getTicketNumber() . PHP_EOL;// will print new value, but not saved since latest backup is commented
// OOPS, its mistake, lets restore old data...
$ticketActions->restore();
$ticketActions->restore();
$ticketActions->restore();
$ticketActions->restore();