From 72a621a9d3bbe8e157f970d6c080a2e51b6fcc52 Mon Sep 17 00:00:00 2001 From: Ollie Harridge Date: Thu, 24 Apr 2014 21:52:38 +0100 Subject: [PATCH 1/2] Improving the createEvent method and adding some defaults and docblock --- Intercom.php | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/Intercom.php b/Intercom.php index 2693cd1..8768922 100644 --- a/Intercom.php +++ b/Intercom.php @@ -449,16 +449,33 @@ public function updateTag($name, } - public function createEvent($event_name, $user_email_or_id, $createdAt, $meta_data = []) { + /** + * @param $event_name The name of the event (required) + * @param $user_email_or_id Email or user id (required) + * @param array $meta_data Array of data to store with the event (optional) + * @param string $createdAt Timestamp or string (optional, defaults to now) + * @return object + */ + public function createEvent($event_name, + $user_email_or_id, + $meta_data = [], + $createdAt = 'now') + { $path = "events"; $data = ["event_name" => $event_name]; + if(!is_numeric($createdAt)) { + $createdAt = strtotime($createdAt); + if(!$createdAt) { + $createdAt = time(); + } + } - if(is_numeric($user_email_or_id)) { - $data["user_id"] = $user_email_or_id; + if($this->isEmail($user_email_or_id)) { + $data["email"] = $user_email_or_id; } else { - $data["email"] = $user_email_or_id; + $data["user_id"] = $user_email_or_id; } $data["created"] = $createdAt; From 5620bef935449d2a03f718865db5c9ef409f8a4c Mon Sep 17 00:00:00 2001 From: Ollie Harridge Date: Fri, 25 Apr 2014 21:19:05 +0100 Subject: [PATCH 2/2] tidying up --- Intercom.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Intercom.php b/Intercom.php index 8768922..a7ebedf 100644 --- a/Intercom.php +++ b/Intercom.php @@ -481,7 +481,7 @@ public function createEvent($event_name, $data["created"] = $createdAt; $data["metadata"] = $meta_data; - return $this->httpCall($this->apiEndpoint . $path, 'POST', json_encode($data)); + return $this->httpCall(str_replace('v1/', '', $this->apiEndpoint) . $path, 'POST', json_encode($data)); } }