/classes/processor.php

https://bitbucket.org/nbaxevanis/p360-dev-test · PHP · 168 lines · 100 code · 27 blank · 41 comment · 12 complexity · bfb3bb74d0eaf4088468b87f91b4d77b MD5 · raw file

  1. <?php
  2. /**
  3. * processor class is the main engine for the application
  4. * It checks to see if server is on a state to be able to work without any issues
  5. * and is able to parse the uploaded file and generate our graph
  6. */
  7. class processor {
  8. const _GRAPH_WIDTH = 500;
  9. const _GRAPH_HEIGHT = 400;
  10. const _GRAPH_TITLE = 'P360 sample graph';
  11. const _GRAPH_DOT_COLOR = 'red';
  12. const _GRAPH_DOT_WIDTH = 8;
  13. const _FILE_STORAGE_DIR = '../uploads/';
  14. const _FILE_NAME = 'data.csv';
  15. private $criteria = array();
  16. private $loadedData = false;
  17. /**
  18. * This method will build a list with all the unique filter types available from the file
  19. *
  20. * @return array
  21. */
  22. private function _generateUniqueFilterOptions() {
  23. $data = $this->_getDataFromFile();
  24. $options = array();
  25. foreach ($data as $entry) {
  26. array_push($options, $entry[0]);
  27. }
  28. return array_unique($options);
  29. }
  30. /**
  31. * This method will parse the csv file and generate the loadedData array with the values from the file
  32. * if this method is already called (thus the file is already parsed) it will return the existing data
  33. *
  34. * @return array
  35. */
  36. private function _getDataFromFile() {
  37. if (!$this->loadedData) {
  38. $this->loadedData = array();
  39. ini_set("auto_detect_line_endings", true); //bug on mac computers :)
  40. if (($handle = fopen(self::_FILE_STORAGE_DIR . self::_FILE_NAME, "r")) !== FALSE) {
  41. while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) {
  42. array_push($this->loadedData, $data);
  43. }
  44. fclose($handle);
  45. }
  46. }
  47. return $this->loadedData;
  48. }
  49. /**
  50. * This method will get an array with wanted filters and validate them agains the valid options found in the file
  51. * Then it will generate a unique list with the wanted filters and setup the criteria variable
  52. *
  53. * @param array $filters
  54. */
  55. public function validateAndHandleFilters(array $filters) {
  56. $uniqueOptions = $this->_generateUniqueFilterOptions();
  57. foreach ($filters as $value) {
  58. if (in_array($value, $uniqueOptions)) {
  59. array_push($this->criteria, $value);
  60. }
  61. }
  62. $this->criteria = array_unique($this->criteria);
  63. }
  64. /**
  65. * This method will speak to the graph builder in order to setup and build the graph that we want
  66. * at the end based on the render flag we can take the image as results or just setup the graph to make sure that everything is working alright
  67. *
  68. * @param bool $render
  69. */
  70. public function renderGraph($render = true) {
  71. $datax = array();
  72. $datay = array();
  73. $allData = $this->_getDataFromFile();
  74. foreach ($allData as $entry) {
  75. if (in_array($entry[0], $this->criteria)) {
  76. array_push($datax, (int) $entry[1]);
  77. array_push($datay, (int) $entry[2]);
  78. }
  79. }
  80. $graph = new Graph(self::_GRAPH_HEIGHT, self::_GRAPH_HEIGHT);
  81. $graph->SetScale("linlin");
  82. $graph->img->SetMargin(40, 40, 40, 40);
  83. $graph->SetShadow();
  84. $graph->title->Set(self::_GRAPH_TITLE);
  85. $graph->title->SetFont(FF_FONT1, FS_BOLD);
  86. $sp1 = new ScatterPlot($datay, $datax);
  87. $sp1->mark->SetType(MARK_FILLEDCIRCLE);
  88. $sp1->mark->SetFillColor(self::_GRAPH_DOT_COLOR);
  89. $sp1->mark->SetWidth(self::_GRAPH_DOT_WIDTH);
  90. $graph->Add($sp1);
  91. if ($render) {
  92. $graph->Stroke();
  93. }
  94. }
  95. /**
  96. * This method was made in order to validate some basic things before the we start working with the main app.
  97. * Some basic checks that we can inform user to solve before moving on
  98. *
  99. * @return json response
  100. */
  101. public function checkSystem() {
  102. $error = 'Unknown';
  103. $status = 'error';
  104. if (!file_exists(self::_FILE_STORAGE_DIR)) {
  105. $error = 'Upload directory does not exist. Please create a directory named `' . self::_FILE_STORAGE_DIR . '`';
  106. } else {
  107. if (!is_writable(self::_FILE_STORAGE_DIR)) {
  108. $error = 'Upload directory is not writable. Please make sure that we can write into this directory `' . self::_FILE_STORAGE_DIR . '`';
  109. } else {
  110. //lets create a demo file just to make sure that everything is ok
  111. $saved = file_put_contents(self::_FILE_STORAGE_DIR . self::_FILE_NAME, '"type1",1,2' . PHP_EOL . '"type2",3,4');
  112. if (!$saved) {
  113. $error = 'There was an unknown error while trying to create a file into this directory `' . self::_FILE_STORAGE_DIR . '`';
  114. } else {
  115. //lets make a final check that we can generate a graph without any issues
  116. $this->criteria = array('type1', 'type2');
  117. try {
  118. $this->renderGraph(false);
  119. $status = 'ok';
  120. $error = false;
  121. unlink(self::_FILE_STORAGE_DIR . self::_FILE_NAME);
  122. } catch (Exception $e) {
  123. $error = 'There was an error trying to create the graph';
  124. }
  125. }
  126. }
  127. }
  128. header('Content-type: application/json');
  129. echo json_encode(array('status' => $status, 'reason' => $error));
  130. }
  131. /**
  132. * This method will return a json response with all the available filters found in the uploaded file
  133. *
  134. * @return json response
  135. */
  136. public function getFilters() {
  137. $uniqueOptions = $this->_generateUniqueFilterOptions();
  138. header('Content-type: application/json');
  139. echo json_encode(array('status' => 'ok', 'options' => $uniqueOptions));
  140. }
  141. }