<?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);
}
}