This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Making
ExamInterface
implement Iterator
.
Adding dummy methods to not break tests for now.
- Loading branch information
Showing
2 changed files
with
202 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
<?php | ||
namespace PHPPeru\Exam; | ||
|
||
use Symfony\Component\EventDispatcher\EventDispatcherInterface, | ||
BadMethodCallException; | ||
|
||
/** | ||
* Descrobes a minimal API required for the concept of Exam | ||
* | ||
* @author Marco Pivetta <ocramius@gmail.com> | ||
* @todo add methods to the interface besides the ones used as examples | ||
*/ | ||
interface ExamInterface | ||
{ | ||
/** | ||
* Begins the exam. | ||
* | ||
* @throws BadMethodCallException if the exam can't be started | ||
*/ | ||
public function start(); | ||
|
||
/** | ||
* Aborts the exam. | ||
* | ||
* @throws BadMethodCallException if the exam can't be aborted | ||
*/ | ||
public function abort(); | ||
|
||
/** | ||
* Completes the exam | ||
* | ||
* @throws BadMethodCallException if the exam can't be completed | ||
*/ | ||
public function complete(); | ||
<?php | ||
namespace PHPPeru\Exam; | ||
|
||
use Symfony\Component\EventDispatcher\EventDispatcherInterface, | ||
BadMethodCallException; | ||
|
||
/** | ||
* Descrobes a minimal API required for the concept of Exam | ||
* | ||
* @author Marco Pivetta <ocramius@gmail.com> | ||
* @todo add methods to the interface besides the ones used as examples | ||
*/ | ||
interface ExamInterface extends Iterator | ||
{ | ||
/** | ||
* Begins the exam. | ||
* | ||
* @throws BadMethodCallException if the exam can't be started | ||
*/ | ||
public function start(); | ||
|
||
/** | ||
* Aborts the exam. | ||
* | ||
* @throws BadMethodCallException if the exam can't be aborted | ||
*/ | ||
public function abort(); | ||
|
||
/** | ||
* Completes the exam | ||
* | ||
* @throws BadMethodCallException if the exam can't be completed | ||
*/ | ||
public function complete(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,134 +1,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; | ||
} | ||
<?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'); | ||
} | ||
} |