Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $start = date("Y-m-d", strtotime("now"));
- $end = date("Y-m-d", strtotime("+3 hours"));
- $form = new stdObject;
- $form->st = $start;
- $form->et = $end;
- $processor = new EPGProcessor($form);
- $packages = $processor->process();
- class EPGProcessor
- {
- private $form;
- private $st;
- private $et;
- private $ipp;
- private $pg;
- public function __construct($form)
- {
- $this->form = $this->setupForm($form);
- $this->factory = new EpgFactory();
- }
- private function setupForm($form)
- {
- foreach (['st', 'et', 'pg', 'ipp'] as $key) {
- $this->$key = (int)$form->$key;
- $form->$key = false;
- }
- return $form;
- }
- public function process()
- {
- $queue = $this->buildQueue();
- $collection = $this->processQueue();
- $provider = $this->getDataProvider($collection);
- return $provider;
- }
- private function getDataProvider(array $collection) {
- $dataProvider=new CArrayDataProvider($collection, array(
- 'id'=>'epg',
- 'sort'=>array(
- 'attributes'=>array(
- 'title','startTime'
- ),
- ),
- 'pagination'=>array(
- 'pageSize'=>$this->ipp,
- 'currentPage'=>$this->pg
- ),
- )
- );
- return $dataProvider;
- }
- private function getMidnightDate($timestamp)
- {
- $day = date("Y-m-d", $timestamp);
- $date = new DateTime($day . ' 00:00:00');
- return $date;
- }
- private function getDateRange(DateTime $day)
- {
- $start = $this->getMidnightDate($day->getTimestamp());
- $end = clone $start;
- $end->modify("+1 day");
- return [$start->getTimestamp(), $end->getTimestamp()];
- }
- private function dateDiff(DateTime $dayStart, DateTime $dayEnd)
- {
- $diff = $dayEnd->diff($dayStart)->format("%a");
- return $diff;
- }
- private function processQueue($queue)
- {
- $collection = [];
- foreach ($queue as $day => $timestamps) {
- $form = $this->form;
- $form->st = $timestamps[0];
- $form->et = $timestamps[1];
- if ($package = $this->factory->getPackage($form) === true) {
- $this->processPackage($package, $collection);
- } else {
- $package = $this->factory->buildPackage($form);
- $this->factory->storePackage($package, $form);
- $this->processPackage($package, $collection);
- }
- }
- return $collection;
- }
- private function processPackage(EpgPackage $package, &$collection) {
- $form = $this->form;
- $form->st = $this->st;
- $form->et = $this->et;
- $collection = $collection + $package->getData($form);
- }
- private function buildQueue()
- {
- $st = $this->getMidnightDate($this->st);
- $et = $this->getMidnightDate($this->et);
- $days = 1;
- $diff = $this->dateDiff($st, $et);
- $days += $diff;
- $queue = [];
- for ($i = 0; $i < $days; $i++) {
- $day = $st->format("Y-m-d");
- $queue[$day] = $this->getDateRange($st);
- $st->modify("+1 day");
- $i++;
- }
- return $queue;
- }
- }
- class EPGPackage {
- private $start;
- private $end;
- private $data;
- public function getAll() {
- return $this->data;
- }
- public function getRange($start, $end) {
- }
- public function normalize() {
- }
- public function setPagination($pagination) {
- $this->pagination = $data['pagination'];
- }
- public function setForm($form) {
- $this->start = $form->st;
- $this->end = $form->end;
- }
- public function getData($form) {
- if ($this->start < $form->st && $this->end > $form->et) {
- return $this->getAll();
- } else {
- return $this->getRange($form->st, $form->et);
- }
- }
- public function setData(array $data) {
- $this->data = $this->normalize($data['res']);
- }
- }
- class EPGPackageFactory {
- private function buildPackage($form)
- {
- $data = EPGFactory::getEpgData($form);
- $package = new EPGPackage();
- $package->setData($data);
- $package->setForm($form);
- return $package;
- }
- private function storePackage(EPGPackage $data, $form)
- {
- $expiration = $form->et;
- $cacheKey = $this->buildCacheKey($form);
- return Yii::app()->cacheManager->set($cacheKey, $data, $expiration, false, ['gzip' => true]);
- }
- private function getPackage($form)
- {
- $key = $this->buildCacheKey($form);
- if ($package = Yii::app()->cacheManager->get($key, ['gzip' => true])) {
- return $package;
- }
- return false;
- }
- private function buildCacheKey($form)
- {
- $day = date("Y-m-d", $form->st);
- $key = md5(json_encode([$form->attributes, $day]));
- return $key;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement