From 59ee279b0a2e91965981cf208de32dc82317c867 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 6 Mar 2016 10:57:24 +0100 Subject: [PATCH 1/2] Using PSR6 cache --- Cache/Dummy.php | 84 ------------------- .../HappyrGoogleAnalyticsExtension.php | 14 +++- Resources/config/services.yml | 4 - Service/DataFetcher.php | 57 ++++++++----- Upgrade.md | 4 +- composer.json | 2 + 6 files changed, 49 insertions(+), 116 deletions(-) delete mode 100644 Cache/Dummy.php diff --git a/Cache/Dummy.php b/Cache/Dummy.php deleted file mode 100644 index 2c61457..0000000 --- a/Cache/Dummy.php +++ /dev/null @@ -1,84 +0,0 @@ - - */ -class Dummy extends CacheProvider -{ - /** - * Fetches an entry from the cache. - * - * @param string $id The id of the cache entry to fetch. - * - * @return string|bool The cached data or FALSE, if no cache entry exists for the given id. - */ - protected function doFetch($id) - { - return false; - } - - /** - * Tests if an entry exists in the cache. - * - * @param string $id The cache id of the entry to check for. - * - * @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise. - */ - protected function doContains($id) - { - return false; - } - - /** - * Puts data into the cache. - * - * @param string $id The cache id. - * @param string $data The cache entry/data. - * @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this - * cache entry (0 => infinite lifeTime). - * - * @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise. - */ - protected function doSave($id, $data, $lifeTime = 0) - { - return true; - } - - /** - * Deletes a cache entry. - * - * @param string $id The cache id. - * - * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise. - */ - protected function doDelete($id) - { - return true; - } - - /** - * Flushes all cache entries. - * - * @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise. - */ - protected function doFlush() - { - return true; - } - - /** - * Retrieves cached information from the data store. - * - * @since 2.2 - * - * @return array|null An associative array with server's statistics if available, NULL otherwise. - */ - protected function doGetStats() - { - return; - } -} diff --git a/DependencyInjection/HappyrGoogleAnalyticsExtension.php b/DependencyInjection/HappyrGoogleAnalyticsExtension.php index 7b14058..a99d297 100644 --- a/DependencyInjection/HappyrGoogleAnalyticsExtension.php +++ b/DependencyInjection/HappyrGoogleAnalyticsExtension.php @@ -2,6 +2,7 @@ namespace Happyr\GoogleAnalyticsBundle\DependencyInjection; +use Cache\Adapter\Void\VoidCachePool; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; use Symfony\Component\HttpKernel\DependencyInjection\Extension; @@ -43,12 +44,17 @@ public function load(array $configs, ContainerBuilder $container) ->replaceArgument(1, new Reference($config['http_message_factory'])); } - if ($config['fetching']['cache_service']) { - $container->getDefinition('happyr.google_analytics.data_fetcher') - ->replaceArgument(0, new Reference($config['fetching']['cache_service'])); + if (!empty($config['fetching']['cache_service'])) { + $cacheService = $config['fetching']['cache_service']; + } else { + $cacheService = 'happyr.google_analytics.cache.void'; + $container->register($cacheService, VoidCachePool::class); } - if ($config['fetching']['client_service']) { + $container->getDefinition('happyr.google_analytics.data_fetcher') + ->replaceArgument(0, new Reference($cacheService)); + + if (!empty($config['fetching']['client_service'])) { $container->getDefinition('happyr.google_analytics.data_fetcher') ->replaceArgument(1, new Reference($config['fetching']['client_service'])); } else { diff --git a/Resources/config/services.yml b/Resources/config/services.yml index f02e320..4b0280d 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -12,10 +12,6 @@ services: public: false arguments: [~, ~, "%happyr.google_analytics.param.endpoint%"] - happyr.google_analytics.cache.dummy: - class: Happyr\GoogleAnalyticsBundle\Cache\Dummy - arguments: [] - happyr.google_analytics.clientIdProvider: class: Happyr\GoogleAnalyticsBundle\Service\ClientIdProvider arguments: ["@request_stack"] diff --git a/Service/DataFetcher.php b/Service/DataFetcher.php index 959cedc..340a171 100644 --- a/Service/DataFetcher.php +++ b/Service/DataFetcher.php @@ -2,7 +2,7 @@ namespace Happyr\GoogleAnalyticsBundle\Service; -use Doctrine\Common\Cache\CacheProvider; +use Psr\Cache\CacheItemPoolInterface; /** * This service fetches data from the API. @@ -12,7 +12,7 @@ class DataFetcher { /** - * @var \Doctrine\Common\Cache\CacheProvider cache + * @var CacheItemPoolInterface cache */ protected $cache; @@ -32,12 +32,12 @@ class DataFetcher protected $cacheLifetime; /** - * @param CacheProvider $cache - * @param \Google_Client $client - * @param int $viewId - * @param int $cacheLifetime seconds + * @param CacheItemPoolInterface $cache + * @param \Google_Client $client + * @param int $viewId + * @param int $cacheLifetime seconds */ - public function __construct(CacheProvider $cache, \Google_Client $client, $viewId, $cacheLifetime) + public function __construct(CacheItemPoolInterface $cache, \Google_Client $client, $viewId, $cacheLifetime) { $this->cache = $cache; $this->client = $client; @@ -46,29 +46,38 @@ public function __construct(CacheProvider $cache, \Google_Client $client, $viewI } /** - * return the page views for the given url. + * Get page views for the given url. * - * @param string $uri - * @param string $since date on format ('Y-m-d') - * @param string $regex + * @param string $uri + * @param \DateTime|null $startTime + * @param \DateTime|null $endTime + * @param string $regex * * @return int */ - public function getPageViews($uri, $since = null, $regex = '$') + public function getPageViews($uri, \DateTime $startTime = null, \DateTime $endTime = null, $regex = '$') { if (empty($this->viewId)) { throw new \LogicException('You need to specify a profile id that we are going to fetch page views from'); } - if (!$since) { - //one year ago - $since = date('Y-m-d', time() - 86400 * 365); + if ($startTime === null) { + // one year ago + $startTime = new \DateTime('-1year'); } + if ($endTime === null) { + // today + $endTime = new \DateTime(); + } + + $start = $startTime->format('Y-m-d'); + $end = $endTime->format('Y-m-d'); + //create the cache key - $cacheKey = md5($uri.$regex.$since); - $this->cache->setNamespace('PageStatistics.PageViews'); - if (false === $visits = $this->cache->fetch($cacheKey)) { + $cacheKey = sha1($uri.$regex.$start); + $item = $this->cache->getItem($cacheKey); + if (!$item->isHit()) { //check if we got a token if (null === $this->client->getAccessToken()) { return 0; @@ -82,8 +91,8 @@ public function getPageViews($uri, $since = null, $regex = '$') $analytics = new \Google_Service_Analytics($this->client); $results = $analytics->data_ga->get( 'ga:'.$this->viewId, - $since, - date('Y-m-d'), + $start, + $end, 'ga:pageviews', array('filters' => 'ga:pagePath=~^'.$uri.$regex) ); @@ -94,10 +103,12 @@ public function getPageViews($uri, $since = null, $regex = '$') $visits = 0; } - //save cache (TTL 1h) - $this->cache->save($cacheKey, $visits, $this->cacheLifetime); + //save cache item + $item->set($visits) + ->expiresAfter($this->cacheLifetime); + $this->cache->save($item); } - return $visits; + return $item->get(); } } diff --git a/Upgrade.md b/Upgrade.md index 2be1d16..c1ccccc 100644 --- a/Upgrade.md +++ b/Upgrade.md @@ -2,4 +2,6 @@ * Class parameters has been removed to comply with the new Symfony best practices. * Updated namespace from `Happyr\Google\AnalyticsBundle` to `Happyr\GoogleAnalyticsBundle`. -* Updated service names to include an underscore. Previous: `happyr.google.analytics.data_fetcher` Now: `happyr.google_analytics.data_fetcher` \ No newline at end of file +* Updated service names to include an underscore. Previous: `happyr.google.analytics.data_fetcher` Now: `happyr.google_analytics.data_fetcher` +* New method signature for `DataFetcher::getPageViews` +* We do now use PSR6 cache and Httplug for HTTP messaging. \ No newline at end of file diff --git a/composer.json b/composer.json index c14f89b..063aba5 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,8 @@ ], "require": { "php": "^5.4|^7.0", + "psr/cache": "^1.0", + "cache/void-adapter": "^0.3", "php-http/httplug": "^1.0", "php-http/message-factory": "^1.0", "symfony/framework-bundle": "^2.7|^3.0" From 1bba3797e0065904a260c21d72fb0dd5c510794d Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 6 Mar 2016 11:03:32 +0100 Subject: [PATCH 2/2] Drop support for php 5.4 --- .travis.yml | 1 - composer.json | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 509e469..a5eb780 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: php sudo: false php: - - 5.4 - 5.5 - 5.6 - 7.0 diff --git a/composer.json b/composer.json index 063aba5..78e9bed 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ } ], "require": { - "php": "^5.4|^7.0", + "php": "^5.5|^7.0", "psr/cache": "^1.0", "cache/void-adapter": "^0.3", "php-http/httplug": "^1.0",