Skip to content

Commit

Permalink
Merge pull request #4 from thijskok/revised-events
Browse files Browse the repository at this point in the history
Revised events
  • Loading branch information
thijskok committed Mar 9, 2021
2 parents f4f0006 + fd33b45 commit 4f389f0
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 158 deletions.
10 changes: 4 additions & 6 deletions src/Actions/ManagesCompanies.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace TestMonitor\Custify\Actions;

use TestMonitor\Custify\Validator;
use TestMonitor\Custify\Resources\Person;
use TestMonitor\Custify\Resources\Company;
use TestMonitor\Custify\Exceptions\NotFoundException;
use TestMonitor\Custify\Transforms\TransformsCompanies;
Expand Down Expand Up @@ -45,7 +44,7 @@ public function company(string $id): Company

Validator::keysExists($response, ['companies']);

// Simulate a not found response when the user_id does not exists.
// Simulate a not found response when the id does not exists.
if (empty($response['companies'])) {
throw new NotFoundException();
}
Expand All @@ -68,7 +67,7 @@ public function companyByCompanyId(string $companyId): Company

Validator::keysExists($response, ['companies']);

// Simulate a not found response when the user_id does not exists.
// Simulate a not found response when the company_id does not exists.
if (empty($response['companies'])) {
throw new NotFoundException();
}
Expand All @@ -92,17 +91,16 @@ public function createOrUpdateCompany(Company $company): Company
}

/**
* Delete a person.
* Delete a company.
*
* @param \TestMonitor\Custify\Resources\Company $company
*
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException
* @return bool
*/
public function deleteCompany(Company $company)
{
$response = $this->delete("company/{$company->id}");

return is_array($response) && array_key_exists('deleted', $response) && $response['deleted'] === 1;
return (bool) ($response['deleted'] ?? false);
}
}
49 changes: 5 additions & 44 deletions src/Actions/ManagesEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,23 @@
namespace TestMonitor\Custify\Actions;

use TestMonitor\Custify\Resources\Event;
use TestMonitor\Custify\Resources\Person;
use TestMonitor\Custify\Resources\Company;
use TestMonitor\Custify\Resources\Resource;
use TestMonitor\Custify\Transforms\TransformsEvents;

trait ManagesEvents
{
use TransformsEvents;

/**
* Create an event.
* Insert an event.
*
* @param \TestMonitor\Custify\Resources\Event $event
*
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException
* @return bool
*/
public function createEvent(Event $event)
public function insertEvent(Event $event): bool
{
$this->post('event', ['json' => $this->toCustifyEvent($event)]);
$response = $this->post('event', ['json' => $this->toCustifyEvent($event)]);

return $event;
}

/**
* Fire an event.
*
* @param string $name
* @param $resource
* @param array $metadata
*
* @return mixed
*/
public function event(string $name, Resource $resource, array $metadata = [])
{
$data = [
'created_at' => (new \DateTime())->format('Y-m-d h:i:s'),
'name' => $name,
'metadata' => (object) $metadata,
];

if ($resource instanceof Company) {
$data['company_id'] = $resource->company_id;
}

if ($resource instanceof Person && $resource->user_id) {
$data['user_id'] = $resource->user_id;
}

if ($resource instanceof Person && $resource->email) {
$data['email'] = $resource->email;
}

$event = new Event($data);

$this->post('event', ['json' => $this->toCustifyEvent($event)]);

return $event;
return empty(json_decode($response, JSON_FORCE_OBJECT));
}
}
3 changes: 1 addition & 2 deletions src/Actions/ManagesPeople.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,12 @@ public function createOrUpdatePerson(Person $person): Person
*
* @param \TestMonitor\Custify\Resources\Person $person
*
* @throws \TestMonitor\Custify\Exceptions\InvalidDataException
* @return bool
*/
public function deletePerson(Person $person)
{
$response = $this->delete("people/{$person->id}");

return is_array($response) && array_key_exists('deleted', $response) && $response['deleted'] === 1;
return (bool) ($response['deleted'] ?? false);
}
}
43 changes: 21 additions & 22 deletions src/Resources/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,37 @@ class Event extends Resource
public $name;

/**
* The time the event occurred in your application..
* The time the event occurred in your application.
*
* @var string
*/
public $created_at;

/**
* @var string
*/
public $user_id;

/**
* @var string
* The person for which the event occurs.
*
* @var \TestMonitor\Custify\Resources\Person|null
*/
public $email;
public $person;

/**
* @var string
* The company for which the event occurs.
*
* @var \TestMonitor\Custify\Resources\Company|null
*/
public $company_id;
public $company;

/**
* If present, subsequent events with the same identifier will be ignored.
*
* @var string
*/
public $deduplication_id;

/**
* @var array
* Meta data about this event.
*
* @var \TestMonitor\Custify\Resources\MetaData
*/
public $metadata;

Expand All @@ -50,19 +53,15 @@ class Event extends Resource
*/
public function __construct(array $attributes)
{
$this->name = $attributes['name'] ?? '';
$this->created_at = $attributes['created_at'] ?? (new \DateTime())->format('Y-m-d h:i:s');
$this->name = $attributes['name'];

$this->deduplication_id = $attributes['deduplication_id'] ?? '';
$this->person = $attributes['person'] ?? null;
$this->company = $attributes['company'] ?? null;

$this->created_at = $attributes['created_at'] ?? date('c');

$this->metadata = $attributes['metadata'] ?? [];
$this->deduplication_id = $attributes['deduplication_id'] ?? '';

if (array_key_exists('user_id', $attributes)) {
$this->user_id = $attributes['user_id'];
} elseif (array_key_exists('email', $attributes)) {
$this->email = $attributes['email'];
} elseif (array_key_exists('company_id', $attributes)) {
$this->company_id = $attributes['company_id'];
}
$this->metadata = $attributes['metadata'] ?? new MetaData();
}
}
56 changes: 56 additions & 0 deletions src/Resources/MetaData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace TestMonitor\Custify\Resources;

class MetaData extends Resource
{
/**
* The metadata.
*
* @var array
*/
public $metadata;

/**
* Create a new resource instance.
*
* @param array $metadata
*/
public function __construct($metadata = [])
{
$this->metadata = $metadata;
}

/**
* Gets a meta data item.
*
* @param $name
*
* @return mixed|null
*/
public function __get($name)
{
return $this->metadata[$name] ?? null;
}

/**
* Sets a meta data item.
*
* @param string $name
* @param mixed $value
*/
public function __set($name, $value)
{
$this->metadata[$name] = $value;
}

/**
* Returns the custom attributes as a key/value array.
*
* @return object
*/
public function toObject(): object
{
return (object) $this->metadata;
}
}
12 changes: 6 additions & 6 deletions src/Transforms/TransformsEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ trait TransformsEvents
*/
protected function toCustifyEvent(Event $event): array
{
return [
return array_filter([
'name' => $event->name,
'created_at' => $event->created_at,
'user_id' => $event->user_id,
'email' => $event->email,
'company_id' => $event->company_id,
'email' => $event->person->email ?? null,
'user_id' => $event->person->user_id ?? null,
'company_id' => $event->company->company_id ?? null,

'metadata' => $event->metadata ?? [],
];
'metadata' => $event->metadata->toObject(),
]);
}
}
84 changes: 6 additions & 78 deletions tests/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ public function it_should_send_an_event_for_a_person()
$service->shouldReceive('request')->once()->andReturn($response);

// When
$response = $custify->createEvent(new Event([
'user_id' => $this->person['user_id'],
$response = $custify->insertEvent(new Event([
'name' => 'Event',
'person' => new Person($this->person),
]));

// Then
$this->assertInstanceOf(Event::class, $response);
$this->assertTrue($response);
}

/** @test */
Expand All @@ -70,84 +70,12 @@ public function it_should_send_an_event_for_a_company()
$service->shouldReceive('request')->once()->andReturn($response);

// When
$response = $custify->createEvent(new Event([
'company_id' => $this->company['company_id'],
$response = $custify->insertEvent(new Event([
'name' => 'Event',
'company' => new Company($this->company),
]));

// Then
$this->assertInstanceOf(Event::class, $response);
}

/** @test */
public function it_should_trigger_an_event_for_a_person()
{
// Given
$custify = new Client($this->token);

$custify->setClient($service = Mockery::mock('\GuzzleHttp\Client'));

$response = Mockery::mock('Psr\Http\Message\ResponseInterface');
$response->shouldReceive('getStatusCode')->andReturn(202);
$response->shouldReceive('getBody')->andReturn(json_encode([]));

$service->shouldReceive('request')->once()->andReturn($response);

// When
$response = $custify->event('Person created', new Person($this->person));

// Then
$this->assertInstanceOf(Event::class, $response);
$this->assertEquals('Person created', $response->name);
}

/** @test */
public function it_should_trigger_an_event_for_a_company()
{
// Given
$custify = new Client($this->token);

$custify->setClient($service = Mockery::mock('\GuzzleHttp\Client'));

$response = Mockery::mock('Psr\Http\Message\ResponseInterface');
$response->shouldReceive('getStatusCode')->andReturn(202);
$response->shouldReceive('getBody')->andReturn(json_encode([]));

$service->shouldReceive('request')->once()->andReturn($response);

// When
$response = $custify->event('Company created', new Company($this->company));

// Then
$this->assertInstanceOf(Event::class, $response);
$this->assertEquals('Company created', $response->name);
}

/** @test */
public function it_should_trigger_an_event_and_include_metadata()
{
// Given
$custify = new Client($this->token);

$custify->setClient($service = Mockery::mock('\GuzzleHttp\Client'));

$response = Mockery::mock('Psr\Http\Message\ResponseInterface');
$response->shouldReceive('getStatusCode')->andReturn(202);
$response->shouldReceive('getBody')->andReturn(json_encode([]));

$service->shouldReceive('request')->once()->andReturn($response);

// When
$response = $custify->event(
'Company created',
new Company($this->company),
['hello' => 'World']
);

// Then
$this->assertInstanceOf(Event::class, $response);
$this->assertEquals('Company created', $response->name);
$this->assertObjectHasAttribute('hello', $response->metadata);
$this->assertEquals('World', $response->metadata->hello);
$this->assertTrue($response);
}
}

0 comments on commit 4f389f0

Please sign in to comment.