Skip to content
This repository has been archived by the owner on Jul 17, 2020. It is now read-only.

Commit

Permalink
Making ExamInterface implement Iterator.
Browse files Browse the repository at this point in the history
Adding dummy methods to not break tests for now.
  • Loading branch information
Ocramius committed Mar 10, 2012
1 parent 6695dfb commit 47d8f17
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 167 deletions.
68 changes: 34 additions & 34 deletions lib/PHPPeru/Exam/ExamInterface.php
@@ -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();
}
301 changes: 168 additions & 133 deletions lib/PHPPeru/Exam/SimpleExam.php
@@ -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');
}
}

0 comments on commit 47d8f17

Please sign in to comment.