This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
/
SimpleExam.php
169 lines (149 loc) · 3.83 KB
/
SimpleExam.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
<?php
namespace PHPPeru\Exam;
use Symfony\Component\EventDispatcher\EventDispatcherInterface,
Symfony\Component\EventDispatcher\EventDispatcher,
BadMethodCallException;
/**
* Provides a simple concrete exam object that is able to trigger events during
* its lifecycle.
*
* @author Marco Pivetta <ocramius@gmail.com>
* @todo eventually define if ".pre" or ".post" events should be used.
*/
class SimpleExam implements ExamInterface
{
const STATUS_NEW = 0;
const STATUS_STARTED = 1;
const STATUS_ABORTED = 2;
const STATUS_COMPLETED = 4;
const EVENT_START = 'start';
const EVENT_ABORT = 'abort';
const EVENT_COMPLETE = 'complete';
/**
* Event dispatcher used internally to trigger events during lifecycle
*
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Defines the current status of the exam
*
* @var type
*/
protected $status = self::STATUS_NEW;
/**
* Default constructor, initializes events
*/
public function __construct()
{
$this->eventDispatcher = new EventDispatcher();
}
/**
* {@inheritdoc}
*/
public function start()
{
if (!$this->isNew()) {
throw new BadMethodCallException('Exam is not new');
}
$this->status = self::STATUS_STARTED;
$this->eventDispatcher->dispatch(self::EVENT_START, new Event($this));
}
/**
* {@inheritdoc}
*/
public function abort()
{
if (!$this->isStarted()) {
throw new BadMethodCallException('Exam is not started');
}
$this->status = self::STATUS_ABORTED;
$this->eventDispatcher->dispatch(self::EVENT_ABORT, new Event($this));
}
/**
* {@inheritdoc}
*/
public function complete()
{
if (!$this->isStarted()) {
throw new BadMethodCallException('Exam is not started');
}
$this->status = self::STATUS_COMPLETED;
$this->eventDispatcher->dispatch(self::EVENT_COMPLETE, new Event($this));
}
/**
* Checks if the exam is new
*
* @return bool
*/
public function isNew()
{
return $this->status === self::STATUS_NEW;
}
/**
* Checks if the exam has been started
*
* @return bool
*/
public function isStarted()
{
return $this->status === self::STATUS_STARTED;
}
/**
* Checks if the exam has been aborted
*
* @return bool
*/
public function isAborted()
{
return $this->status === self::STATUS_ABORTED;
}
/**
* Checks if the exam has been completed
*
* @return bool
*/
public function isCompleted()
{
return $this->status === self::STATUS_COMPLETED;
}
/**
* Retrieves the associated event dispatcher
*
* @return EventDispatcherInterface
*/
public function getEventDispatcher()
{
return $this->eventDispatcher;
}
/**
* {@inheritdoc}
*/
public function current() {
throw new BadMethodCallException('Not implemented');
}
/**
* {@inheritdoc}
*/
public function key() {
throw new BadMethodCallException('Not implemented');
}
/**
* {@inheritdoc}
*/
public function next() {
throw new BadMethodCallException('Not implemented');
}
/**
* {@inheritdoc}
*/
public function rewind() {
throw new BadMethodCallException('Not implemented');
}
/**
* {@inheritdoc}
*/
public function valid() {
throw new BadMethodCallException('Not implemented');
}
}