From fa856f731df44988e32825910b43a5a379725b98 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 22 Nov 2014 19:37:03 +0100 Subject: [PATCH 1/4] Youtube service --- app/config/parameters.yml.dist | 2 + composer.json | 3 +- composer.lock | 59 ++++++++++++++++--- .../Controller/DefaultController.php | 6 +- .../SiteBundle/Resources/config/services.yml | 7 +++ .../SiteBundle/Service/YoutubeService.php | 57 ++++++++++++++++++ 6 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php diff --git a/app/config/parameters.yml.dist b/app/config/parameters.yml.dist index de4f02d..1595062 100644 --- a/app/config/parameters.yml.dist +++ b/app/config/parameters.yml.dist @@ -17,3 +17,5 @@ parameters: meetup_key: akismet_key: group_urlname: + youtube_key: + youtube_playlist: diff --git a/composer.json b/composer.json index c8c4598..9071d6b 100644 --- a/composer.json +++ b/composer.json @@ -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": "~1.0" }, "require-dev": { "phpspec/phpspec": "2.0.*@dev", diff --git a/composer.lock b/composer.lock index ee6e8de..b25aa95 100644 --- a/composer.lock +++ b/composer.lock @@ -1,9 +1,10 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" + "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" ], - "hash": "b5ceeac0e534a646afff7bc68face043", + "hash": "17f79f7403a1869c3ea07c098a4b9f42", "packages": [ { "name": "antimattr/google-bundle", @@ -1299,6 +1300,51 @@ ], "time": "2013-05-19 03:41:15" }, + { + "name": "madcoda/php-youtube-api", + "version": "v1.0", + "source": { + "type": "git", + "url": "https://github.com/madcoda/php-youtube-api.git", + "reference": "b4d888316974012c5d98d85bf4bca1c6fe06b8be" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/madcoda/php-youtube-api/zipball/b4d888316974012c5d98d85bf4bca1c6fe06b8be", + "reference": "b4d888316974012c5d98d85bf4bca1c6fe06b8be", + "shasum": "" + }, + "require": { + "php": ">5.3" + }, + "require-dev": { + "phpunit/phpunit": "3.7.*@stable" + }, + "type": "library", + "autoload": { + "psr-0": { + "Madcoda": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Madcoda", + "email": "jason@madcoda.com" + } + ], + "description": "PHP wrapper for the Youtube Data API v3", + "keywords": [ + "api", + "madcoda", + "video", + "youtube" + ], + "time": "2014-07-07 04:59:39" + }, { "name": "monolog/monolog", "version": "1.7.0", @@ -3044,9 +3090,7 @@ "time": "2012-12-03 16:58:27" } ], - "aliases": [ - - ], + "aliases": [], "minimum-stability": "stable", "stability-flags": { "dms/meetup-api-bundle": 20, @@ -3058,10 +3102,9 @@ "antimattr/google-bundle": 20, "phpspec/phpspec": 20 }, + "prefer-stable": false, "platform": { "php": ">=5.3.3" }, - "platform-dev": [ - - ] + "platform-dev": [] } diff --git a/src/AmsterdamPHP/Bundle/SiteBundle/Controller/DefaultController.php b/src/AmsterdamPHP/Bundle/SiteBundle/Controller/DefaultController.php index 6bbfa1c..7d4f100 100644 --- a/src/AmsterdamPHP/Bundle/SiteBundle/Controller/DefaultController.php +++ b/src/AmsterdamPHP/Bundle/SiteBundle/Controller/DefaultController.php @@ -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, ]; } } diff --git a/src/AmsterdamPHP/Bundle/SiteBundle/Resources/config/services.yml b/src/AmsterdamPHP/Bundle/SiteBundle/Resources/config/services.yml index 5241f9c..abecfed 100644 --- a/src/AmsterdamPHP/Bundle/SiteBundle/Resources/config/services.yml +++ b/src/AmsterdamPHP/Bundle/SiteBundle/Resources/config/services.yml @@ -5,6 +5,13 @@ services: arguments: cache: @snc_redis.cache + amsterdamphp_site.integration.youtube: + class: AmsterdamPHP\Bundle\SiteBundle\Service\YoutubeService + arguments: + cache: @snc_redis.cache + key: %youtube_key% + playlist: %youtube_playlist% + amsterdamphp_site.email.contact: class: AmsterdamPHP\Bundle\SiteBundle\Service\ContactService arguments: diff --git a/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php b/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php new file mode 100644 index 0000000..369afce --- /dev/null +++ b/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php @@ -0,0 +1,57 @@ +client = new Youtube(array( + 'key' => $key, + )); + $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('+5 hours')); + } + + $videoList = unserialize(base64_decode($videoList)); + + return $videoList; + } +} From 24f6cc0360587f6def0f58717797da86fd2ecd8e Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Mon, 24 Nov 2014 20:30:17 +0100 Subject: [PATCH 2/4] Moved the cache TTL into a class constant --- src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php b/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php index 369afce..bac30dc 100644 --- a/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php +++ b/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php @@ -7,6 +7,8 @@ class YoutubeService { + const CACHE_TTL = '+24 hours'; + /** * @var Client */ @@ -47,7 +49,7 @@ public function getLatestVideos() if ($videoList === null){ $videoList = base64_encode(serialize($this->client->getPlaylistItemsByPlaylistId($this->playlist))); $this->cache->set($cacheKey, $videoList); - $this->cache->expireat($cacheKey, strtotime('+5 hours')); + $this->cache->expireat($cacheKey, strtotime(self::CACHE_TTL)); } $videoList = unserialize(base64_decode($videoList)); From b9da4889f51a41076d6819af521d9cd09c7f69e5 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 18 Apr 2015 12:07:28 +0200 Subject: [PATCH 3/4] Using dev-master for now --- composer.json | 2 +- composer.lock | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 9071d6b..739c65d 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "ornicar/akismet-bundle": "*@dev", "ornicar/gravatar-bundle": "dev-master@dev", "antimattr/google-bundle": "dev-master@dev", - "madcoda/php-youtube-api": "~1.0" + "madcoda/php-youtube-api": "dev-master" }, "require-dev": { "phpspec/phpspec": "2.0.*@dev", diff --git a/composer.lock b/composer.lock index b25aa95..57016f9 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "17f79f7403a1869c3ea07c098a4b9f42", + "hash": "67db699695bf374e22836fc9dd6a44c4", "packages": [ { "name": "antimattr/google-bundle", @@ -1302,16 +1302,16 @@ }, { "name": "madcoda/php-youtube-api", - "version": "v1.0", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/madcoda/php-youtube-api.git", - "reference": "b4d888316974012c5d98d85bf4bca1c6fe06b8be" + "reference": "673f75882e7c44df9ca304cff07048e0781ea6ac" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/madcoda/php-youtube-api/zipball/b4d888316974012c5d98d85bf4bca1c6fe06b8be", - "reference": "b4d888316974012c5d98d85bf4bca1c6fe06b8be", + "url": "https://api.github.com/repos/madcoda/php-youtube-api/zipball/673f75882e7c44df9ca304cff07048e0781ea6ac", + "reference": "673f75882e7c44df9ca304cff07048e0781ea6ac", "shasum": "" }, "require": { @@ -1343,7 +1343,7 @@ "video", "youtube" ], - "time": "2014-07-07 04:59:39" + "time": "2015-04-08 15:48:17" }, { "name": "monolog/monolog", @@ -3100,9 +3100,11 @@ "ornicar/akismet-bundle": 20, "ornicar/gravatar-bundle": 20, "antimattr/google-bundle": 20, + "madcoda/php-youtube-api": 20, "phpspec/phpspec": 20 }, "prefer-stable": false, + "prefer-lowest": false, "platform": { "php": ">=5.3.3" }, From 1d4a1745d7c82abefcf0e9c7589be1c034a1749b Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sat, 18 Apr 2015 12:57:38 +0200 Subject: [PATCH 4/4] Factory method as suggested by @pascaldevink and @rdohms --- .../SiteBundle/Resources/config/services.yml | 15 ++++++++- .../SiteBundle/Service/YoutubeFactory.php | 31 +++++++++++++++++++ .../SiteBundle/Service/YoutubeService.php | 8 ++--- 3 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeFactory.php diff --git a/src/AmsterdamPHP/Bundle/SiteBundle/Resources/config/services.yml b/src/AmsterdamPHP/Bundle/SiteBundle/Resources/config/services.yml index abecfed..96a3518 100644 --- a/src/AmsterdamPHP/Bundle/SiteBundle/Resources/config/services.yml +++ b/src/AmsterdamPHP/Bundle/SiteBundle/Resources/config/services.yml @@ -1,4 +1,17 @@ +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 @@ -9,7 +22,7 @@ services: class: AmsterdamPHP\Bundle\SiteBundle\Service\YoutubeService arguments: cache: @snc_redis.cache - key: %youtube_key% + client: @amsterdamphp_site.integration.youtube_client playlist: %youtube_playlist% amsterdamphp_site.email.contact: diff --git a/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeFactory.php b/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeFactory.php new file mode 100644 index 0000000..5df9993 --- /dev/null +++ b/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeFactory.php @@ -0,0 +1,31 @@ +key = $key; + } + + /** + * @return Youtube + */ + public function getClient() + { + return new Youtube(array( + 'key' => $this->key, + )); + } +} \ No newline at end of file diff --git a/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php b/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php index bac30dc..e6e2b76 100644 --- a/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php +++ b/src/AmsterdamPHP/Bundle/SiteBundle/Service/YoutubeService.php @@ -10,7 +10,7 @@ class YoutubeService const CACHE_TTL = '+24 hours'; /** - * @var Client + * @var Youtube */ protected $client; @@ -27,11 +27,9 @@ class YoutubeService /** * Constructor */ - public function __construct(Cache $cache, $key, $playlist) + public function __construct(Cache $cache, Youtube $client, $playlist) { - $this->client = new Youtube(array( - 'key' => $key, - )); + $this->client = $client; $this->cache = $cache; $this->playlist = $playlist; }