Skip to content

Commit

Permalink
Completed people & event producers.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterborodatyy committed Apr 2, 2017
1 parent 67fad64 commit e7be818
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 18 deletions.
34 changes: 24 additions & 10 deletions src/Producers/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,47 @@

namespace Mintance\Producers;

use Mintance\Exceptions\Exception;

class Event extends AbstractProducer {

public function track($name, array $params = []) {
$response = $this->_push($this->_buildEvent($name, 'custom-event', $params));

$event = $this->_buildEvent($name, 'custom-event', $params);

$this->_push($event);
if(!empty($response['event_id'])) {
return $response['event_id'];
} else {
throw new Exception('Event sending error.');
}
}

public function charge($amount, array $params = []) {

$event = $this->_buildEvent('Charge', 'charge', array_merge($params, [
$response = $this->_push($this->_buildEvent('Charge', 'charge', array_merge($params, [
'value' => $amount
]));
])));

$this->_push($event);
if(!empty($response['charge_id'])) {
return $response['charge_id'];
} else {
throw new Exception('Charge sending error.');
}
}

public function formSubmit(array $data) {
$event = array_merge(

$response = $this->_push(array_merge(
$this->_buildEvent('Form Submit', 'form-submit'),
[
'form_data' => $data
]
);
));

$this->_push($event);
if(!empty($response['event_id'])) {
return $response['event_id'];
} else {
throw new Exception('Charge sending error.');
}
}

protected function _buildEvent($name, $type, array $params = []) {
Expand All @@ -42,6 +56,6 @@ protected function _buildEvent($name, $type, array $params = []) {
protected function _push(array $event) {
$this->_transport->setEndpoint('events');

$this->_transport->execute($event);
return $this->_transport->execute($event);
}
}
56 changes: 48 additions & 8 deletions src/Producers/People.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Mintance\Producers;

use Mintance\Exceptions\Exception;
use Mintance\Transport\AbstractTransport;

class People extends AbstractProducer {
Expand All @@ -12,10 +13,16 @@ class People extends AbstractProducer {

protected $_people_id;

protected $_identifier;

protected $_type = 'visitor';

protected $_people = [];

protected $_default_fields = [
'name', 'first_name', 'last_name', 'email', 'phone'
];

public function __construct(AbstractTransport $transport) {
parent::__construct($transport);

Expand All @@ -24,16 +31,53 @@ public function __construct(AbstractTransport $transport) {
$this->_subscribe();
}

public function identify($identifier) {
public function setIdentifier($identifier) {

$this->_identifier = $identifier;

$this->_transport->register('execute:before', function (&$args) {
if(empty($this->_people_id)) {
$args['mintance_identifier'] = $this->_identifier;
}
});
}

public function set(array $args) {
public function identify($identifier) {

if(!empty($this->_people_id)) {
return $this->_people_id;
}

if(empty($this->_visitor_id)) {
throw new Exception('Empty visitor_id.');
}

$this->_transport->setEndpoint('people/identify');

$response = $this->_transport->execute([
'visitor_id' => $this->_visitor_id,
'identifier' => $identifier
]);

$this->_people = $response['people'];

$this->_type = 'people';

return $this->_people_id = $response['people_id'];
}

public function __set($key, $value) {
// TODO: Implement __set() method.
public function set(array $args) {
$data = [
'fields' => []
];

foreach ($args as $key => $value) {
if(in_array($key, $this->_default_fields)) {
$data[$key] = $value;
} else {
$data['fields'][$key] = $value;
}
}
}

public function get() {
Expand All @@ -44,10 +88,6 @@ public function get() {
]);
}

public function __get($key) {
// TODO: Implement __get() method.
}

protected function _subscribe() {
$this->_transport->register('execute:before', function (&$args) {
if(!empty($this->_people_id)) {
Expand Down

0 comments on commit e7be818

Please sign in to comment.