From c8eaee79a111904f02224bf467392d0e4747b9ab Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Sat, 17 Dec 2011 00:59:30 +0000 Subject: [PATCH] Add entity classes Add object wrappers for the application database entities. --- source/lib/StatusBoard/Incident.class.php | 139 ++++++++++++++++++ .../lib/StatusBoard/IncidentStatus.class.php | 12 ++ source/lib/StatusBoard/Service.class.php | 99 +++++++++++++ source/lib/StatusBoard/Site.class.php | 120 +++++++++++++++ source/lib/StatusBoard/Status.class.php | 29 ++++ 5 files changed, 399 insertions(+) create mode 100644 source/lib/StatusBoard/Incident.class.php create mode 100644 source/lib/StatusBoard/IncidentStatus.class.php create mode 100644 source/lib/StatusBoard/Service.class.php create mode 100644 source/lib/StatusBoard/Site.class.php create mode 100644 source/lib/StatusBoard/Status.class.php diff --git a/source/lib/StatusBoard/Incident.class.php b/source/lib/StatusBoard/Incident.class.php new file mode 100644 index 0000000..4bf30bf --- /dev/null +++ b/source/lib/StatusBoard/Incident.class.php @@ -0,0 +1,139 @@ +id = $id; + $this->site = $site; + $this->reference = $reference; + $this->description = $description; + $this->start_time = $start_time; + $this->estimated_end_time = $estimated_end_time; + $this->actual_end_time = $actual_end_time; + } + + public static function fromDatabaseRow($row) { + return new self( + $row['id'], + $row['site'], + $row['reference'], + $row['description'], + $row['start_time'], + $row['estimated_end_time'], + $row['actual_end_time'] + ); + } + + /** + * Load an Incident object given its ID + * + * @param int $id + * @return StatusBoard_Incident + */ + public static function fromId($id) { + $database = StatusBoard_Main::instance()->database(); + + $incident = self::fromDatabaseRow( + $database->selectOne('SELECT * FROM `incident` WHERE id=:id', array( + array('name' => 'id', 'value' => $id, 'type' => PDO::PARAM_INT) + ) + ) + ); + + return $incident; + } + + public static function all() { + $incidents = array(); + + $database = StatusBoard_Main::instance()->database(); + foreach ($database->selectList('SELECT * FROM `incident` WHERE `id` > 0 ORDER BY `id` DESC') as $row) { + $incidents[] = self::fromDatabaseRow($row); + } + + return $incidents; + } + + public static function open_for_site(StatusBoard_Site $site) { + $incidents = array(); + + $database = StatusBoard_Main::instance()->database(); + foreach ($database->selectList('SELECT * FROM `incident_open` WHERE `site`=:site ORDER BY `id` DESC', array( + array('name' => 'site', 'value' => $site->id(), 'type' => PDO::PARAM_INT), + )) as $row) { + $incidents[] = self::fromDatabaseRow($row); + } + + return $incidents; + } + + protected function create() { + $database = StatusBoard_Main::instance()->database(); + $database->insert( + 'INSERT INTO `service` + (`id`, `site`, `reference`, `description`, `start_time`, `estimated_end_time`, `actual_end_time`) + VALUES(NULL, :site, :reference, :description, :start_time, :estimated_end_time, :actual_end_time)', + array( + array('name' => 'site', 'value' => $this->site, 'type' => PDO::PARAM_INT), + array('name' => 'reference', 'value' => $this->reference, 'type' => PDO::PARAM_STR), + array('name' => 'description', 'value' => $this->description, 'type' => PDO::PARAM_STR), + array('name' => 'start_time', 'value' => $this->start_time, 'type' => PDO::PARAM_INT), + array('name' => 'estimated_end_time', 'value' => $this->estimated_end_time, 'type' => PDO::PARAM_INT), + array('name' => 'actual_end_time', 'value' => $this->actual_end_time, 'type' => PDO::PARAM_INT), + ) + ); + + $this->id = $database->lastInsertId(); + } + + public function delete() { + $database = StatusBoard_Main::instance()->database(); + $database->update( + 'DELETE FROM `incident` WHERE `id`=:id LIMIT 1', + array( + array('name' => 'id', 'value' => $this->id, 'type' => PDO::PARAM_INT), + ) + ); + + $this->id = null; + } + + public function id() { + return $this->id; + } + + public function site() { + return $this->site; + } + + public function reference() { + return $this->reference; + } + + public function description() { + return $this->description; + } + + public function start_time() { + return $this->start_time; + } + + public function estimated_end_time() { + return $this->estimated_end_time; + } + + public function actual_end_time() { + return $this->actual_end_time; + } + +} + +?> diff --git a/source/lib/StatusBoard/IncidentStatus.class.php b/source/lib/StatusBoard/IncidentStatus.class.php new file mode 100644 index 0000000..2aac330 --- /dev/null +++ b/source/lib/StatusBoard/IncidentStatus.class.php @@ -0,0 +1,12 @@ + \ No newline at end of file diff --git a/source/lib/StatusBoard/Service.class.php b/source/lib/StatusBoard/Service.class.php new file mode 100644 index 0000000..b120ae7 --- /dev/null +++ b/source/lib/StatusBoard/Service.class.php @@ -0,0 +1,99 @@ +id = $id; + $this->name = $name; + $this->description = $description; + } + + public static function fromDatabaseRow($row) { + return new self( + $row['id'], + $row['name'], + $row['description'] + ); + } + + /** + * Load a Service object given its ID + * + * @param int $id + * @return StatusBoard_Service + */ + public static function fromId($id) { + $database = StatusBoard_Main::instance()->database(); + + $service = self::fromDatabaseRow( + $database->selectOne('SELECT * FROM `service` WHERE id=:id', array( + array('name' => 'id', 'value' => $id, 'type' => PDO::PARAM_INT) + ) + ) + ); + + return $service; + } + + public static function all() { + $services = array(); + + $database = StatusBoard_Main::instance()->database(); + foreach ($database->selectList('SELECT * FROM `service` WHERE `id` > 0 ORDER BY `id` DESC') as $row) { + $services[] = self::fromDatabaseRow($row); + } + + return $services; + } + + protected function create() { + $database = StatusBoard_Main::instance()->database(); + $database->insert( + 'INSERT INTO `service` + (`id`, `name`, `description`) + VALUES(NULL, :name, :description)', + array( + array('name' => 'name', 'value' => $this->name, 'type' => PDO::PARAM_STR), + array('name' => 'description', 'value' => $this->description, 'type' => PDO::PARAM_STR), + ) + ); + + $this->id = $database->lastInsertId(); + } + + public function delete() { + $database = StatusBoard_Main::instance()->database(); + $database->update( + 'DELETE FROM `service` WHERE `id`=:id LIMIT 1', + array( + array('name' => 'id', 'value' => $this->id, 'type' => PDO::PARAM_INT), + ) + ); + + $this->id = null; + } + + public function sites() { + return StatusBoard_Site::all_for_service($this); + } + + public function id() { + return $this->id; + } + + public function name() { + return $this->name; + } + + public function description() { + return $this->description; + } + + +} + +?> \ No newline at end of file diff --git a/source/lib/StatusBoard/Site.class.php b/source/lib/StatusBoard/Site.class.php new file mode 100644 index 0000000..ae6e77e --- /dev/null +++ b/source/lib/StatusBoard/Site.class.php @@ -0,0 +1,120 @@ +id = $id; + $this->service = $service; + $this->name = $name; + $this->description = $description; + } + + public static function fromDatabaseRow($row) { + return new self( + $row['id'], + $row['service'], + $row['name'], + $row['description'] + ); + } + + /** + * Load a Site object given its ID + * + * @param int $id + * @return StatusBoard_Site + */ + public static function fromId($id) { + $database = StatusBoard_Main::instance()->database(); + + $site = self::fromDatabaseRow( + $database->selectOne('SELECT * FROM `site` WHERE id=:id', array( + array('name' => 'id', 'value' => $id, 'type' => PDO::PARAM_INT) + ) + ) + ); + + return $site; + } + + public static function all() { + $sites = array(); + + $database = StatusBoard_Main::instance()->database(); + foreach ($database->selectList('SELECT * FROM `site` WHERE `id` > 0 ORDER BY `id` DESC') as $row) { + $sites[] = self::fromDatabaseRow($row); + } + + return $sites; + } + + public function all_for_service(StatusBoard_Service $service) { + $sites = array(); + + $database = StatusBoard_Main::instance()->database(); + foreach ($database->selectList('SELECT * FROM `site` WHERE `service`=:service ORDER BY `id` DESC', array( + array('name' => 'service', 'value' => $service->id(), 'type' => PDO::PARAM_INT), + )) as $row) { + $sites[] = self::fromDatabaseRow($row); + } + + return $sites; + } + + protected function create() { + $database = StatusBoard_Main::instance()->database(); + $database->insert( + 'INSERT INTO `service` + (`id`, `service`, `name`, `description`) + VALUES(NULL, :service, :name, :description)', + array( + array('name' => 'service', 'value' => $this->service, 'type' => PDO::PARAM_INT), + array('name' => 'name', 'value' => $this->name, 'type' => PDO::PARAM_STR), + array('name' => 'description', 'value' => $this->description, 'type' => PDO::PARAM_STR), + ) + ); + + $this->id = $database->lastInsertId(); + } + + public function delete() { + $database = StatusBoard_Main::instance()->database(); + $database->update( + 'DELETE FROM `site` WHERE `id`=:id LIMIT 1', + array( + array('name' => 'id', 'value' => $this->id, 'type' => PDO::PARAM_INT), + ) + ); + + $this->id = null; + } + + public function incidents_open() { + return StatusBoard_Incident::open_for_site($this); + } + + public function id() { + return $this->id; + } + + public function service() { + return $this->service; + } + + public function name() { + return $this->name; + } + + public function description() { + return $this->description; + } + + +} + +?> \ No newline at end of file diff --git a/source/lib/StatusBoard/Status.class.php b/source/lib/StatusBoard/Status.class.php new file mode 100644 index 0000000..7a2cc71 --- /dev/null +++ b/source/lib/StatusBoard/Status.class.php @@ -0,0 +1,29 @@ + 'The service is operating normally.', + self::STATUS_Maintenance => 'The service is undergoing scheduled maintenance.', + self::STATUS_Minor => 'The service is exeriencing minor issues affecting some customers.', + self::STATUS_Significant => 'The service is exeriencing significant issues affecting many customers.', + self::STATUS_Major => 'The service is exeriencing a major outage affecting all customers.', + ); + + public static function description($status) { + if ( ! StatusBoard_Main::isClassConstantValue(self, 'STATUS_', $status)) { + throw new StatusBoard_Exception_InvalidParameters($status); + } + + return self::$descriptions[$status]; + } + +} + +?> \ No newline at end of file