/lib/IBL/RotationMapper.php

https://github.com/chartjes/building-testable-applications · PHP · 206 lines · 169 code · 33 blank · 4 comment · 15 complexity · e23e48402b0c2551c4290863f0c25da0 MD5 · raw file

  1. <?php
  2. namespace IBL;
  3. class RotationMapper
  4. {
  5. protected $_conn;
  6. protected $_map = array();
  7. public function __construct($conn = null)
  8. {
  9. if ($conn !== null) {
  10. $this->_conn = $conn;
  11. }
  12. // Load our class mapper from the XML config file
  13. $fields = simplexml_load_file(LIB_ROOT . 'ibl/maps/rotation.xml');
  14. foreach ($fields as $field) {
  15. $this->_map[(string)$field->name] = $field;
  16. }
  17. }
  18. public function createRotationFromRow($row)
  19. {
  20. $rotation = new \IBL\Rotation($this);
  21. foreach ($this->_map as $field) {
  22. $setProp = (string)$field->mutator;
  23. $value = trim($row[(string)$field->name]);
  24. if ($setProp && $value) {
  25. call_user_func(array($rotation, $setProp), $value);
  26. }
  27. }
  28. return $rotation;
  29. }
  30. public function delete(\IBL\Rotation $rotation)
  31. {
  32. if ($rotation->getId() == null) {
  33. return false;
  34. }
  35. try {
  36. $sql = "DELETE FROM rotations WHERE id = ?";
  37. $sth = $this->_conn->prepare($sql);
  38. $sth->execute(array((int)$rotation->getId()));
  39. } catch (\PDOException $e) {
  40. echo "DB Error: " . $e->getMessage();
  41. }
  42. }
  43. public function findByFranchiseId($franchiseId)
  44. {
  45. try {
  46. $sql = "SELECT * FROM rotations WHERE franchise_id = ?";
  47. $sth = $this->_conn->prepare($sql);
  48. $sth->execute(array((int)$franchiseId));
  49. $row = $sth->fetch();
  50. if ($row) {
  51. return $this->createRotationFromRow($row);
  52. }
  53. } catch (\PDOException $e) {
  54. echo "DB Error: " . $e->getMessage();
  55. }
  56. return false;
  57. }
  58. public function findById($id)
  59. {
  60. try {
  61. $sql = "SELECT * FROM rotations WHERE id = ?";
  62. $sth = $this->_conn->prepare($sql);
  63. $sth->execute(array((int)$id));
  64. $row = $sth->fetch();
  65. if ($row) {
  66. return $this->createRotationFromRow($row);
  67. }
  68. } catch (\PDOException $e) {
  69. echo "DB Error: " . $e->getMessage();
  70. }
  71. return false;
  72. }
  73. public function findByWeek($week)
  74. {
  75. try {
  76. $sql = "SELECT * FROM rotations WHERE week = ?";
  77. $sth = $this->_conn->prepare($sql);
  78. $sth->execute(array((int)$week));
  79. $rows = $sth->fetchAll();
  80. $rotations = array();
  81. if ($rows) {
  82. foreach ($rows as $row) {
  83. $rotations[] = $this->createRotationFromRow($row);
  84. }
  85. }
  86. return $rotations;
  87. } catch (\PDOException $e) {
  88. echo 'DB_Error: ' . $e->getMessage();
  89. }
  90. }
  91. public function generateRotations($rotations, $franchises)
  92. {
  93. if (count($rotations) == 0) {
  94. return array();
  95. }
  96. $response = array();
  97. $franchiseInfo = array();
  98. foreach ($franchises as $franchise) {
  99. $franchiseInfo[$franchise->getId()] = $franchise->getNickname();
  100. }
  101. foreach ($rotations as $rotation) {
  102. $response[$franchiseInfo[$rotation->getFranchiseId()]] =
  103. $rotation->getRotation();
  104. }
  105. ksort($response);
  106. return $response;
  107. }
  108. public function save(\IBL\rotation $rotation)
  109. {
  110. if ($rotation->getId()) {
  111. $this->_update($rotation);
  112. } else {
  113. $this->_insert($rotation);
  114. }
  115. }
  116. protected function _insert(\IBL\rotation $rotation)
  117. {
  118. try {
  119. // Of course, Postgres has to do things a little differently
  120. // and we cannot use lastInsertId() so you alter the INSERT
  121. // statement to return the insert ID
  122. $sql = "
  123. INSERT INTO rotations
  124. (week, rotation, franchise_id)
  125. VALUES(?, ?, ?) RETURNING id";
  126. $sth = $this->_conn->prepare($sql);
  127. $binds = array(
  128. $rotation->getWeek(),
  129. $rotation->getRotation(),
  130. $rotation->getFranchiseId()
  131. );
  132. $response = $sth->execute($binds);
  133. $result = $sth->fetch(\PDO::FETCH_ASSOC);
  134. if ($result['id']) {
  135. $inserted = $this->findById($result['id']);
  136. if (method_exists($inserted, 'setId')) {
  137. $rotation->setId($result['id']);
  138. }
  139. } else {
  140. throw new \Exception('Unable to create new Rotation record');
  141. }
  142. } catch(\PDOException $e) {
  143. echo "A database problem occurred: " . $e->getMessage();
  144. }
  145. }
  146. protected function _update(\IBL\rotation $rotation)
  147. {
  148. try {
  149. $sql = "
  150. UPDATE rotations
  151. SET week = ?,
  152. rotation = ?,
  153. franchise_id = ?
  154. WHERE id = ?";
  155. $sth = $this->_conn->prepare($sql);
  156. $fields = array(
  157. 'week',
  158. 'rotation',
  159. 'franchise_id',
  160. 'id'
  161. );
  162. $binds = array();
  163. foreach ($fields as $fieldName) {
  164. $field = $this->_map[$fieldName];
  165. $getProp = (string)$field->accessor;
  166. $binds[] = $rotation->{$getProp}();
  167. }
  168. $response = $sth->execute($binds);
  169. } catch(\PDOException $e) {
  170. echo "A database problem occurred: " . $e->getMessage();
  171. }
  172. }
  173. }