Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Youtube service #77

Merged
merged 4 commits into from
Apr 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparantly, animattr tagged a couple of versions in the mean time 😀 https://packagist.org/packages/antimattr/google-bundle, can we use a stable version?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ow neat, I'll look into that 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking at the package name it's a different package then what is used for youtube. Maybe it's out of scope for this PR?

"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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No feedback on your code, but we should definitely introduce controllers as services for better dependency injection. That would've also been a nice hackathon ticket 😝

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Controllers as services are overrated to be honest. Too much overhead for not enough benefit.

$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,
];
}
}
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok i'm fine with this, even if you could drop the factory and just intantiate the client directly as a service, but this gives us a place to set defaults if we ever need them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After talking with you this was my assumption as how you preferred to have it setup. (Heck I took you meetup bundle as example.) If this is fine I'll let it be but if you really prefer instantiating the client directly I'll change it to that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine like this 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I/m fine with this way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! 👍


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