Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: allow dependency injection of httpClient #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 75 additions & 15 deletions src/Heap/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,43 @@

namespace Heap;

use GuzzleHttp\ClientInterface;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding support for PSR-18 and using its ClientInterface?

https://www.php-fig.org/psr/psr-18/

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ServerException;
use Heap\Exception\HeapException;
use Heap\Helper\Request;
use Psr\Http\Message\ResponseInterface;

class Client
{
/**
* @var string
*/
private $apiUri = 'https://heapanalytics.com/api';

/**
* @var string
*/
private $appId;

/**
* @var ClientInterface
*/
private $httpClient;

/**
* Client constructor.
*
* @param null $appId
* @param null $appId
* @param ClientInterface|null $httpClient
*/
public function __construct($appId)
public function __construct($appId, ClientInterface $httpClient = null)
{
if (! $httpClient) {
$httpClient = new \GuzzleHttp\Client();
}

$this->appId = $appId;
$this->request = new Request();
$this->httpClient = $httpClient;
}

/**
Expand All @@ -35,20 +56,21 @@ public function setAppId($appId)
}

/**
* @param $event
* @param $identity
* @param $event
* @param $identity
* @param array $properties
*
* @return mixed|\Psr\Http\Message\ResponseInterface
* @return ResponseInterface
* @throws HeapException
* @throws GuzzleException
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Within this scope I see HeapException being thrown.

Why GuzzleException has been added to the doc block?

*/
public function track($event, $identity, $properties = array())
{
if(!$event || empty($event)) {
if (!$event || empty($event)) {
throw new HeapException('You need to set the event name.');
}

if(!$identity || empty($identity)) {
if (!$identity || empty($identity)) {
throw new HeapException('You need to set the identity. More info: https://heapanalytics.com/docs/server-side');
}

Expand All @@ -59,19 +81,20 @@ public function track($event, $identity, $properties = array())
'identity' => $identity,
);

return $this->request->call('POST', '/track', $data);
return $this->call('POST', '/track', $data);
}

/**
* @param $identity
* @param $identity
* @param array $properties
*
* @return mixed|\Psr\Http\Message\ResponseInterface
* @return ResponseInterface
* @throws HeapException
* @throws GuzzleException
*/
public function addUserProperties($identity, $properties = array())
{
if(!$identity || empty($identity)) {
if (!$identity || empty($identity)) {
throw new HeapException('You need to set the identity. More info: https://heapanalytics.com/docs/server-side');
}

Expand All @@ -81,6 +104,43 @@ public function addUserProperties($identity, $properties = array())
'identity' => $identity,
);

return $this->request->call('POST', '/add_user_properties', $data);
return $this->call('POST', '/add_user_properties', $data);
}

/**
* @param $endpoint
*
* @return string
*/
public function generateUri($endpoint)
{
return $this->apiUri.$endpoint;
}

/**
* @param string $method
* @param string $endpoint
* @param array $data
*
* @return ResponseInterface
* @throws HeapException
* @throws GuzzleException
*/
public function call($method, $endpoint, $data)
{
$fullUri = $this->generateUri($endpoint);

try {
$response = $this->httpClient->request($method, $fullUri, array(
'headers' => array(
'Content-Type' => 'application/json',
),
'body' => \GuzzleHttp\json_encode($data),
));
} catch (ServerException $e) {
throw new HeapException($e->getMessage());
}

return $response;
}
}
}
56 changes: 0 additions & 56 deletions src/Heap/Helper/Request.php

This file was deleted.