Skip to content

Commit

Permalink
Merge pull request #15 from Nyholm/cache
Browse files Browse the repository at this point in the history
Using PSR6 cache
  • Loading branch information
Nyholm committed Mar 6, 2016
2 parents a1ed9cf + 1bba379 commit 96e86d0
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 118 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: php
sudo: false

php:
- 5.4
- 5.5
- 5.6
- 7.0
Expand Down
84 changes: 0 additions & 84 deletions Cache/Dummy.php

This file was deleted.

14 changes: 10 additions & 4 deletions DependencyInjection/HappyrGoogleAnalyticsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
57 changes: 34 additions & 23 deletions Service/DataFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Happyr\GoogleAnalyticsBundle\Service;

use Doctrine\Common\Cache\CacheProvider;
use Psr\Cache\CacheItemPoolInterface;

/**
* This service fetches data from the API.
Expand All @@ -12,7 +12,7 @@
class DataFetcher
{
/**
* @var \Doctrine\Common\Cache\CacheProvider cache
* @var CacheItemPoolInterface cache
*/
protected $cache;

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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)
);
Expand All @@ -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();
}
}
4 changes: 3 additions & 1 deletion Upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
* 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.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
}
],
"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",
"php-http/message-factory": "^1.0",
"symfony/framework-bundle": "^2.7|^3.0"
Expand Down

0 comments on commit 96e86d0

Please sign in to comment.