-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Nyholm/httplug
Decouple form Guzzle
- Loading branch information
Showing
10 changed files
with
66 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters