Skip to content

Instantly share code, notes, and snippets.

@Kcko
Created February 22, 2019 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kcko/f7709f4d329f046b6137bb5f6ca624a0 to your computer and use it in GitHub Desktop.
Save Kcko/f7709f4d329f046b6137bb5f6ca624a0 to your computer and use it in GitHub Desktop.
<?php
namespace App\FrontModule\Components;
use App;
use Nette\Application\UI\Control;
use Nette\Caching\Cache;
use Nette\Caching\IStorage;
/**
* Class Youtube
*/
class Youtube extends FrontControl
{
public $presets;
public $content;
public $cacheStorage;
public function __construct(App\Model\Presets $presets, IStorage $IStorage)
{
$this->presets = $presets;
$this->cacheStorage = $IStorage;
}
private function fetchData()
{
$user = $this->presets->defaults['keys']['youtube']['user'];
$path = "https://www.youtube.com/feeds/videos.xml?channel_id=$user";
try {
$out = json_decode(json_encode(@new \SimpleXMLIterator($path, null, true)), TRUE);
return $out;
} catch (\Exception $e) {
return false;
}
}
public function getData()
{
$cache = new Cache($this->cacheStorage, 'youtubePosts');
try {
$out = $cache->load("videos");
} catch (\Exception $e) {
$out = false;
}
if (empty($out)) {
try {
$data = self::fetchData();
if ($data) {
$out = $data['entry'];
$cache->save("videos", $out, array(
Cache::EXPIRATION => '+30 minutes',
Cache::SLIDING => TRUE
));
} else {
$out = false;
}
} catch (\Exception $e) {
$out = false;
}
}
return $out;
}
public function renderDefault(array $config = array())
{
$config = $this->getCurrentConfig($config);
$this->template->presets = $this->presets->getPresets();
$this->template->youtubeData = $this->getData();
$this->render($config);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment