Skip to content

Commit

Permalink
Merge pull request #14 from Nyholm/httplug
Browse files Browse the repository at this point in the history
Decouple form Guzzle
  • Loading branch information
Nyholm committed Mar 6, 2016
2 parents a709fb6 + d5e48d3 commit a1ed9cf
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 145 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ php:
- hhvm

env:
- SYMFONY_VERSION=2.7.*
- SYMFONY_VERSION=2.8.*
- SYMFONY_VERSION=3.0.*

cache:
directories:
Expand All @@ -28,6 +26,8 @@ script:
- phpunit

matrix:
allow_failures:
include:
- env: SYMFONY_VERSION=3.0.*
php: 5.4
php: 5.5
- env: SYMFONY_VERSION=2.7.*
php: 5.5
10 changes: 6 additions & 4 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public function getConfigTreeBuilder()

$rootNode
->children()
->scalarNode('version')->cannotBeEmpty()->defaultValue(1)->end()
->booleanNode('enabled')->defaultTrue()->end()
->booleanNode('enabled')->defaultTrue()->info('If disabled we will not send any data')->end()
->scalarNode('version')->cannotBeEmpty()->defaultValue(1)->info('The version of the Measurement Protocol')->end()
->scalarNode('tracking_id')->isRequired()->cannotBeEmpty()->end()

->scalarNode('endpoint')->defaultValue('http://www.google-analytics.com/collect')->cannotBeEmpty()->end()
->booleanNode('fireAndForget')->defaultFalse()->end()
->floatNode('requestTimeout')->defaultValue(1)->end()
->scalarNode('http_client')->defaultValue('httplug.client')->end()
->scalarNode('http_message_factory')->defaultValue('httplug.message_factory')->end()

->arrayNode('fetching')->addDefaultsIfNotSet()->children()
->integerNode('view_id')->defaultNull()->info('The google analytics view id. This is not the same as the tracking code.')->end()
->scalarNode('cache_service')->defaultNull()->end()
Expand Down
10 changes: 7 additions & 3 deletions DependencyInjection/HappyrGoogleAnalyticsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public function load(array $configs, ContainerBuilder $container)

$base = 'happyr.google_analytics.param.';
$container->setParameter($base.'endpoint', $config['endpoint']);
$container->setParameter($base.'fireAndForget', $config['fireAndForget']);
$container->setParameter($base.'requestTimeout', $config['requestTimeout']);
$container->setParameter($base.'view_id', $config['fetching']['view_id']);
$container->setParameter($base.'cache_lifetime', $config['fetching']['cache_lifetime']);

Expand All @@ -38,7 +36,11 @@ public function load(array $configs, ContainerBuilder $container)
->replaceArgument(3, $config['version']);

if (!$config['enabled']) {
$trackerDef->replaceArgument(0, new Reference('happyr.google_analytics.http.dummy'));
$trackerDef->replaceArgument(0, new Reference('happyr.google_analytics.http.void'));
} else {
$container->getDefinition('happyr.google_analytics.http.client')
->replaceArgument(0, new Reference($config['http_client']))
->replaceArgument(1, new Reference($config['http_message_factory']));
}

if ($config['fetching']['cache_service']) {
Expand All @@ -49,6 +51,8 @@ public function load(array $configs, ContainerBuilder $container)
if ($config['fetching']['client_service']) {
$container->getDefinition('happyr.google_analytics.data_fetcher')
->replaceArgument(1, new Reference($config['fetching']['client_service']));
} else {
$container->removeDefinition('happyr.google_analytics.data_fetcher');
}
}
}
89 changes: 35 additions & 54 deletions Http/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Happyr\GoogleAnalyticsBundle\Http;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Http\Message\MessageFactory;
use Http\Client\HttpClient as HttplugClient;

/**
* This is an adapter for Httplug.
*
* @author Tobias Nyholm <[email protected]>
*/
class HttpClient implements HttpClientInterface
Expand All @@ -18,46 +20,25 @@ class HttpClient implements HttpClientInterface
protected $endpoint;

/**
* @var int requestTimeout
*/
protected $requestTimeout;

/**
* @var bool fireAndForget
*
* Should we bother about the response or not?
*/
protected $fireAndForget;

/**
* @var Client client
* @var HttplugClient client
*/
protected $client;

/**
* @param string $endpoint
* @param bool $fireAndForget
* @param int $requestTimeout
* @var MessageFactory
*/
public function __construct($endpoint, $fireAndForget, $requestTimeout)
{
$this->endpoint = $endpoint;
$this->fireAndForget = $fireAndForget;
$this->requestTimeout = $requestTimeout;
}
protected $messageFactory;

/**
* Get a GuzzleClient.
*
* @return Client
* @param HttplugClient $client
* @param MessageFactory $messageFactory
* @param string $endpoint
*/
protected function getClient()
public function __construct(HttplugClient $client, MessageFactory $messageFactory, $endpoint)
{
if ($this->client === null) {
$this->client = new Client();
}

return $this->client;
$this->endpoint = $endpoint;
$this->client = $client;
$this->messageFactory = $messageFactory;
}

/**
Expand All @@ -69,30 +50,30 @@ protected function getClient()
*/
public function send(array $data = array())
{
$client = $this->getClient();
$options = array(
'body' => $data,
'headers' => array(
'User-Agent' => 'happyr-google-analytics/3.0',
),
'timeout' => $this->requestTimeout,
$request = $this->getMessageFactory()->createRequest(
'POST',
$this->endpoint,
['User-Agent' => 'happyr-google-analytics/4.0'],
http_build_query($data)
);
$response = $this->getClient()->sendRequest($request);

$request = $client->createRequest('POST', $this->endpoint, $options);

// If we should send the async or not.
if ($this->fireAndForget) {
$client->sendAll(array($request));

return true;
}
return $response->getStatusCode() === 200;
}

try {
$response = $client->send($request);
} catch (RequestException $e) {
return false;
}
/**
* @return HttplugClient
*/
protected function getClient()
{
return $this->client;
}

return $response->getStatusCode() == '200';
/**
* @return MessageFactory
*/
protected function getMessageFactory()
{
return $this->messageFactory;
}
}
2 changes: 1 addition & 1 deletion Http/Dummy.php → Http/VoidHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* @author Tobias Nyholm <[email protected]>
*/
class Dummy implements HttpClientInterface
class VoidHttpClient implements HttpClientInterface
{
/**
* This is just a dummy client.. Do nothing.
Expand Down
15 changes: 6 additions & 9 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ services:
class: Happyr\GoogleAnalyticsBundle\Service\Tracker
arguments: ["@happyr.google_analytics.http.client", "@happyr.google_analytics.clientIdProvider", ~, ~]

happyr.google_analytics.http.void:
class: Happyr\GoogleAnalyticsBundle\Http\VoidHttpClient

happyr.google_analytics.http.client:
class: Happyr\GoogleAnalyticsBundle\Http\HttpClient
arguments:
- %happyr.google_analytics.param.endpoint%
- %happyr.google_analytics.param.fireAndForget%
- %happyr.google_analytics.param.requestTimeout%

happyr.google_analytics.http.dummy:
class: Happyr\GoogleAnalyticsBundle\Http\Dummy
arguments: []
public: false
arguments: [~, ~, "%happyr.google_analytics.param.endpoint%"]

happyr.google_analytics.cache.dummy:
class: Happyr\GoogleAnalyticsBundle\Cache\Dummy
Expand All @@ -25,5 +22,5 @@ services:

happyr.google_analytics.data_fetcher:
class: Happyr\GoogleAnalyticsBundle\Service\DataFetcher
arguments: ["@happyr.google_analytics.cache.dummy", ~, %happyr.google_analytics.param.view_id%, %happyr.google_analytics.param.cache_lifetime%]
arguments: ["@happyr.google_analytics.cache.dummy", ~, "%happyr.google_analytics.param.view_id%", "%happyr.google_analytics.param.cache_lifetime%"]

2 changes: 2 additions & 0 deletions Service/ClientIdProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Symfony\Component\HttpFoundation\RequestStack;

/**
* This service tries to fetch a cookie and return Googles client id. The client id is like a user id.
*
* @author Tobias Nyholm <[email protected]>
*/
class ClientIdProvider
Expand Down
6 changes: 1 addition & 5 deletions Service/DataFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@ class DataFetcher
* @param int $viewId
* @param int $cacheLifetime seconds
*/
public function __construct(CacheProvider $cache, \Google_Client $client = null, $viewId, $cacheLifetime)
public function __construct(CacheProvider $cache, \Google_Client $client, $viewId, $cacheLifetime)
{
if (!$client) {
throw new \LogicException('You must install and configure happyr/google-site-authenticator-bundle in order to use the fetching data service.');
}

$this->cache = $cache;
$this->client = $client;
$this->viewId = $viewId;
Expand Down
63 changes: 0 additions & 63 deletions Tests/Http/HttpClientTest.php

This file was deleted.

6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
],
"require": {
"php": "^5.4|^7.0",
"guzzlehttp/guzzle": "~5.0",
"php-http/httplug": "^1.0",
"php-http/message-factory": "^1.0",
"symfony/framework-bundle": "^2.7|^3.0"
},
"suggest": {
"happyr/google-site-authenticator-bundle": "To be able to fetch data from google analytics."
"happyr/google-site-authenticator-bundle": "To be able to fetch data from google analytics.",
"php-http/httplug-bundle": "To register HTTP clients as services"
},
"autoload": {
"psr-4": {
Expand Down

0 comments on commit a1ed9cf

Please sign in to comment.