Skip to content

Commit

Permalink
Merge pull request AmsterdamPHP#77 from WyriHaximus/master
Browse files Browse the repository at this point in the history
Youtube service
  • Loading branch information
Pascal de Vink committed Apr 18, 2015
2 parents 017b2ae + 1d4a174 commit 7773665
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 10 deletions.
2 changes: 2 additions & 0 deletions app/config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ parameters:
meetup_key:
akismet_key:
group_urlname:
youtube_key:
youtube_playlist:
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"guzzle/guzzle": "~3.7",
"ornicar/akismet-bundle": "*@dev",
"ornicar/gravatar-bundle": "dev-master@dev",
"antimattr/google-bundle": "dev-master@dev"
"antimattr/google-bundle": "dev-master@dev",
"madcoda/php-youtube-api": "dev-master"
},
"require-dev": {
"phpspec/phpspec": "2.0.*@dev",
Expand Down
61 changes: 53 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ public function indexAction()
$blogService = $this->get('amsterdamphp_site.integration.blog');
$posts = $blogService->getLatestBlogPosts();

$youtubeService = $this->get('amsterdamphp_site.integration.youtube');
$videos = $youtubeService->getLatestVideos();

return [
'header_photo' => $headerPhoto,
'next_event' => array_shift($nextEvents),
'past_events' => array_splice($pastEvents, 0, 2),
'blog_posts' => $posts,
'sponsors' => $sponsors
'sponsors' => $sponsors,
'videos' => $videos,
];
}

Expand Down
20 changes: 20 additions & 0 deletions src/AmsterdamPHP/Bundle/SiteBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
parameters:
amsterdamphp_site.integration.youtube_factory.class: AmsterdamPHP\Bundle\SiteBundle\Service\YoutubeFactory
amsterdamphp_site.integration.youtube_client.class: Madcoda\Youtube

services:
amsterdamphp_site.integration.youtube_factory:
class: %amsterdamphp_site.integration.youtube_factory.class%
arguments:
key: %youtube_key%

amsterdamphp_site.integration.youtube_client:
class: "%amsterdamphp_site.integration.youtube_client.class%"
factory_service: amsterdamphp_site.integration.youtube_factory
factory_method: getClient

amsterdamphp_site.integration.blog:
class: AmsterdamPHP\Bundle\SiteBundle\Service\BlogService
arguments:
cache: @snc_redis.cache

amsterdamphp_site.integration.youtube:
class: AmsterdamPHP\Bundle\SiteBundle\Service\YoutubeService
arguments:
cache: @snc_redis.cache
client: @amsterdamphp_site.integration.youtube_client
playlist: %youtube_playlist%

amsterdamphp_site.email.contact:
class: AmsterdamPHP\Bundle\SiteBundle\Service\ContactService
arguments:
Expand Down
31 changes: 31 additions & 0 deletions src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace AmsterdamPHP\Bundle\SiteBundle\Service;

use Madcoda\Youtube;

class YoutubeFactory
{
/**
* @var string
*/
protected $key;

/**
* @param string $key
*/
public function __construct($key)
{
$this->key = $key;
}

/**
* @return Youtube
*/
public function getClient()
{
return new Youtube(array(
'key' => $this->key,
));
}
}
57 changes: 57 additions & 0 deletions src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace AmsterdamPHP\Bundle\SiteBundle\Service;

use Madcoda\Youtube;
use Predis\Client as Cache;

class YoutubeService
{
const CACHE_TTL = '+24 hours';

/**
* @var Youtube
*/
protected $client;

/**
* @var Cache
*/
protected $cache;

/**
* @var string
*/
protected $playlist;

/**
* Constructor
*/
public function __construct(Cache $cache, Youtube $client, $playlist)
{
$this->client = $client;
$this->cache = $cache;
$this->playlist = $playlist;
}

/**
* Returns the latest blog posts
*
* @return mixed
*/
public function getLatestVideos()
{
$cacheKey = 'youtube.talks.latest';
$videoList = $this->cache->get($cacheKey);

if ($videoList === null){
$videoList = base64_encode(serialize($this->client->getPlaylistItemsByPlaylistId($this->playlist)));
$this->cache->set($cacheKey, $videoList);
$this->cache->expireat($cacheKey, strtotime(self::CACHE_TTL));
}

$videoList = unserialize(base64_decode($videoList));

return $videoList;
}
}

0 comments on commit 7773665

Please sign in to comment.