Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. <?php
  2.  
  3. $start = date("Y-m-d", strtotime("now"));
  4. $end = date("Y-m-d", strtotime("+3 hours"));
  5. $form = new stdObject;
  6. $form->st = $start;
  7. $form->et = $end;
  8.  
  9. $processor = new EPGProcessor($form);
  10. $packages = $processor->process();
  11.  
  12.  
  13. class EPGProcessor
  14. {
  15.  
  16. private $form;
  17. private $st;
  18. private $et;
  19. private $ipp;
  20. private $pg;
  21.  
  22. public function __construct($form)
  23. {
  24.  
  25. $this->form = $this->setupForm($form);
  26. $this->factory = new EpgFactory();
  27.  
  28. }
  29.  
  30. private function setupForm($form)
  31. {
  32. foreach (['st', 'et', 'pg', 'ipp'] as $key) {
  33. $this->$key = (int)$form->$key;
  34. $form->$key = false;
  35. }
  36. return $form;
  37. }
  38.  
  39. public function process()
  40. {
  41. $queue = $this->buildQueue();
  42. $collection = $this->processQueue();
  43. $provider = $this->getDataProvider($collection);
  44. return $provider;
  45. }
  46.  
  47. private function getDataProvider(array $collection) {
  48.  
  49. $dataProvider=new CArrayDataProvider($collection, array(
  50. 'id'=>'epg',
  51. 'sort'=>array(
  52. 'attributes'=>array(
  53. 'title','startTime'
  54. ),
  55. ),
  56. 'pagination'=>array(
  57. 'pageSize'=>$this->ipp,
  58. 'currentPage'=>$this->pg
  59. ),
  60. )
  61. );
  62.  
  63. return $dataProvider;
  64. }
  65.  
  66. private function getMidnightDate($timestamp)
  67. {
  68. $day = date("Y-m-d", $timestamp);
  69. $date = new DateTime($day . ' 00:00:00');
  70. return $date;
  71. }
  72.  
  73. private function getDateRange(DateTime $day)
  74. {
  75.  
  76. $start = $this->getMidnightDate($day->getTimestamp());
  77. $end = clone $start;
  78. $end->modify("+1 day");
  79.  
  80. return [$start->getTimestamp(), $end->getTimestamp()];
  81. }
  82.  
  83. private function dateDiff(DateTime $dayStart, DateTime $dayEnd)
  84. {
  85. $diff = $dayEnd->diff($dayStart)->format("%a");
  86. return $diff;
  87. }
  88.  
  89. private function processQueue($queue)
  90. {
  91. $collection = [];
  92.  
  93. foreach ($queue as $day => $timestamps) {
  94. $form = $this->form;
  95. $form->st = $timestamps[0];
  96. $form->et = $timestamps[1];
  97.  
  98. if ($package = $this->factory->getPackage($form) === true) {
  99. $this->processPackage($package, $collection);
  100. } else {
  101. $package = $this->factory->buildPackage($form);
  102. $this->factory->storePackage($package, $form);
  103. $this->processPackage($package, $collection);
  104. }
  105. }
  106. return $collection;
  107. }
  108.  
  109. private function processPackage(EpgPackage $package, &$collection) {
  110. $form = $this->form;
  111. $form->st = $this->st;
  112. $form->et = $this->et;
  113. $collection = $collection + $package->getData($form);
  114. }
  115.  
  116. private function buildQueue()
  117. {
  118. $st = $this->getMidnightDate($this->st);
  119. $et = $this->getMidnightDate($this->et);
  120.  
  121. $days = 1;
  122. $diff = $this->dateDiff($st, $et);
  123. $days += $diff;
  124.  
  125. $queue = [];
  126.  
  127. for ($i = 0; $i < $days; $i++) {
  128. $day = $st->format("Y-m-d");
  129. $queue[$day] = $this->getDateRange($st);
  130. $st->modify("+1 day");
  131. $i++;
  132. }
  133.  
  134. return $queue;
  135. }
  136. }
  137.  
  138. class EPGPackage {
  139.  
  140. private $start;
  141. private $end;
  142. private $data;
  143.  
  144. public function getAll() {
  145. return $this->data;
  146. }
  147.  
  148. public function getRange($start, $end) {
  149.  
  150. }
  151.  
  152. public function normalize() {
  153.  
  154. }
  155.  
  156. public function setPagination($pagination) {
  157.  
  158. $this->pagination = $data['pagination'];
  159. }
  160.  
  161. public function setForm($form) {
  162. $this->start = $form->st;
  163. $this->end = $form->end;
  164. }
  165.  
  166. public function getData($form) {
  167. if ($this->start < $form->st && $this->end > $form->et) {
  168. return $this->getAll();
  169. } else {
  170. return $this->getRange($form->st, $form->et);
  171. }
  172. }
  173.  
  174. public function setData(array $data) {
  175.  
  176. $this->data = $this->normalize($data['res']);
  177.  
  178. }
  179.  
  180. }
  181.  
  182. class EPGPackageFactory {
  183.  
  184. private function buildPackage($form)
  185. {
  186. $data = EPGFactory::getEpgData($form);
  187. $package = new EPGPackage();
  188. $package->setData($data);
  189. $package->setForm($form);
  190.  
  191. return $package;
  192. }
  193.  
  194. private function storePackage(EPGPackage $data, $form)
  195. {
  196. $expiration = $form->et;
  197. $cacheKey = $this->buildCacheKey($form);
  198.  
  199. return Yii::app()->cacheManager->set($cacheKey, $data, $expiration, false, ['gzip' => true]);
  200. }
  201.  
  202. private function getPackage($form)
  203. {
  204. $key = $this->buildCacheKey($form);
  205. if ($package = Yii::app()->cacheManager->get($key, ['gzip' => true])) {
  206. return $package;
  207. }
  208.  
  209. return false;
  210. }
  211.  
  212. private function buildCacheKey($form)
  213. {
  214. $day = date("Y-m-d", $form->st);
  215. $key = md5(json_encode([$form->attributes, $day]));
  216.  
  217. return $key;
  218. }
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement