Anonymous Segment.com tracking. Closes #91.
This commit is contained in:
committed by
Joseph Cohen
parent
a63c65a6c3
commit
5c391cc888
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Segment;
|
||||
|
||||
use CachetHQ\Cachet\Facades\Setting;
|
||||
use CachetHQ\Cachet\Models\Setting as SettingModel;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class CacheRepository implements RepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @var \CachetHQ\Cachet\Segment\HttpRepository
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* Instantiates a new instance of the Cache Repository.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Segment\HttpRepository $repository
|
||||
*/
|
||||
public function __construct(RepositoryInterface $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether to use the segment_write_key setting or to fetch a new.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function fetch()
|
||||
{
|
||||
// Firstly, does the setting exist?
|
||||
if (false === ($writeKey = Setting::get('segment_write_key'))) {
|
||||
// No, let's go fetch it.
|
||||
$writeKey = $this->repository->fetch();
|
||||
Setting::set('segment_write_key', $writeKey);
|
||||
} else {
|
||||
// It does, but how old is it?
|
||||
$setting = SettingModel::where('name', 'segment_write_key')->first();
|
||||
|
||||
// It's older than an hour, let's refresh
|
||||
if ($setting->updated_at->lt(Carbon::now()->subHour())) {
|
||||
$writeKey = $this->repository->fetch();
|
||||
|
||||
// Update the setting. This is done manual to make sure updated_at is overwritten.
|
||||
$setting->value = $writeKey;
|
||||
$setting->updated_at = Carbon::now();
|
||||
$setting->save();
|
||||
}
|
||||
}
|
||||
|
||||
return $writeKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Segment;
|
||||
|
||||
use GuzzleHttp\ClientInterface;
|
||||
|
||||
class HttpRepository implements RepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @var \Guzzle\GuzzleClient
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* Instantiates a new instance of the SegmentApi class.
|
||||
*
|
||||
* @param \Guzzle\GuzzleClient $client
|
||||
* @param string $url
|
||||
*/
|
||||
public function __construct(ClientInterface $client, $url)
|
||||
{
|
||||
$this->client = $client;
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the segment_write_key from the given url.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function fetch()
|
||||
{
|
||||
return $this->client->get($this->url)->json()['segment_write_key'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Segment;
|
||||
|
||||
interface RepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Returns the segment_write_key.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function fetch();
|
||||
}
|
||||
Reference in New Issue
Block a user