Last active
May 29, 2023 17:03
-
-
Save austenstrine/6a6b92b1b4ab0a1192dfc462d706d1a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SomeZendeskTicketClass { | |
public function getAndSendTestData() { | |
$reallyLongJsonString = "[{\"phone\":\"7202532407\",\"autorespond\":false,\"subject\":\"New Order #S4481058\",\"alert\":false,\"ip\":\"192.168.3.172\",\"message\":\"<link type=\\\"text\\\/css\\\" href=\\\"..\\\/scp\\\/css\\\/bootstrap.css\\\" rel=\\\"stylesheet\\\">\\n<table class=\\\"table table-striped\\\">\\n\\t<tbody>\\n\\t\\t<tr>\\n\\t\\t\\t<td><strong>Website:<\\\/strong> www.craftdirect.com <\\\/td>\\n\\t\\t\\t<td><strong>OrderID:<\\\/strong> CD000016462<\\\/td>\\n\\t\\t\\t<td><strong>Eclipse:<\\\/strong> S4481058<\\\/td>\\n\\t\\t\\t<td><strong>Date:<\\\/strong> 2023-05-26 13:45:32<\\\/td>\\n\\t\\t<\\\/tr>\\n\\t\\t<tr>\\n\\t\\t\\t<td><strong>ORDER DATE:<\\\/strong> <\\\/td>\\n\\t\\t\\t<td><strong>SHIPPING METHOD:<\\\/strong> CDBM<\\\/td>\\n\\t\\t\\t<td><strong>EXT. MERCHANT ID:<\\\/strong><\\\/td>\\n\\t\\t\\t<td><strong>PAYMENT METHOD:<\\\/strong> paypal_express <\\\/td>\\n\\t\\t<\\\/tr>\\n\\t\\t<tr>\\n\\t\\t\\t<td><strong>NAME:<\\\/strong> Becky Vaile<\\\/td>\\n\\t\\t\\t<td><strong>ADDRESS:<\\\/strong><br>11104 Winona Court<br><br>Westminster, CO<br> US<\\\/td>\\n\\t\\t\\t<td><strong>EMAIL:<\\\/strong> bvaileee03@gmail.com<\\\/td>\\n\\t\\t\\t<td><strong>PHONE:<\\\/strong> 7202532407<\\\/td>\\n\\t\\t<\\\/tr>\\n\\t<\\\/tbody>\\n<\\\/table>\\n\\n<table class=\\\"table table-striped\\\">\\n\\t<tbody>\\n\\t\\t<tr>\\n\\t\\t\\t<td><h5>Product Id<\\\/h5><\\\/td>\\n\\t\\t\\t<td><h5>Qty<\\\/h5><\\\/td>\\n\\t\\t\\t<td><h5>Price<\\\/h5><\\\/td>\\n\\t\\t<\\\/tr>\\n\\n\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t<\\\/tbody> \\n<\\\/table>\\n\",\"email\":\"bvaileee03@gmail.com\",\"created\":\"2023-05-26 13:45:32\",\"eclipseId\":\"S4481058\",\"priorityId\":5,\"name\":\"Becky Vaile\",\"html\":true,\"deptId\":0}]"; | |
$dataArray = json_decode( | |
$reallyLongJsonString | |
,true//The first thing is to make sure you're sending the correct data type! | |
//Passing true here will ensure that json objects are interpreted as php associative arrays, | |
//not as php stdClass objects. | |
); | |
foreach($dataArray as $data) { | |
$this->sendRaw($data); | |
} | |
} | |
//Original function - not bad! But throws an exception when it gets an object instead of an array | |
public function sendRaw($data) { | |
$custom_fields = []; | |
$inputs = [ | |
'subject' => $this->website. ' | Raw Email', | |
'comment' => [ | |
'html_body' => "" | |
], | |
"priority" => "normal", | |
"requester" => [ | |
"email" => "psemailfeed@gmail.com", | |
"name" => "", | |
], | |
'custom_fields' => [] | |
]; | |
$params = $data; | |
//checks for subject | |
if (isset($params['subject'])) { | |
$inputs['subject'] = $this->website. " | ". $params['subject']; | |
unset($params['subject']); | |
} | |
//checks for message content | |
if (isset($params['message'])) { | |
$inputs['comment']['html_body'] = $params['message']; | |
unset($params['message']); | |
} | |
//checks for email | |
if (isset($params['email'])) { | |
array_push($custom_fields, [ | |
"id" => 11949515689239, //IP | |
"value" => $params['email'] | |
]); | |
$inputs['requester']['email'] = $params['email']; | |
unset($params['email']); | |
} | |
//checks for phone | |
if (isset($params['phone'])) { | |
array_push($custom_fields, [ | |
"id" => 11922083929367, //phone | |
"value" => $params['phone'] | |
]); | |
unset($params['phone']); | |
} | |
//checks for eclipseId | |
if (isset($params['eclipseId'])) { | |
array_push($custom_fields, [ | |
"id" => 11922084629527, //eclipseId | |
"value" => $params['eclipseId'] | |
]); | |
unset($params['eclipseId']); | |
} | |
//checks for IP | |
if (isset($params['ip'])) { | |
array_push($custom_fields, [ | |
"id" => 11922084325015, //IP | |
"value" => $params['ip'] | |
]); | |
unset($params['ip']); | |
} | |
//appends other data inputs form the raw email request that is not listed on zendesk | |
if (!empty($params) && (count($params) > 0)) { | |
$inputs['comment']['html_body'] = $inputs['comment']['html_body']." | ".json_encode($params); | |
} | |
$inputs['custom_fields'] = $custom_fields; | |
$ticket = Zendesk::tickets()->create($inputs); | |
return $ticket->ticket->id; | |
} | |
//Improved Version: 1 - Type casting | |
public function sendRawTypeCast($data) { | |
//Sometimes, we don't know what kind of data will be sent to us, or we don't have control | |
//over what gets passed in. In this case, type casting can be a useful tool. | |
$data = (array)$data; | |
//One line added, and this function works as expected. | |
$custom_fields = []; | |
$inputs = [ | |
'subject' => $this->website. ' | Raw Email', | |
'comment' => [ | |
'html_body' => "" | |
], | |
"priority" => "normal", | |
"requester" => [ | |
"email" => "psemailfeed@gmail.com", | |
"name" => "", | |
], | |
'custom_fields' => [] | |
]; | |
$params = $data; | |
//checks for subject | |
if (isset($params['subject'])) { | |
$inputs['subject'] = $this->website. " | ". $params['subject']; | |
unset($params['subject']); | |
} | |
//checks for message content | |
if (isset($params['message'])) { | |
$inputs['comment']['html_body'] = $params['message']; | |
unset($params['message']); | |
} | |
//checks for email | |
if (isset($params['email'])) { | |
array_push($custom_fields, [ | |
"id" => 11949515689239, //IP | |
"value" => $params['email'] | |
]); | |
$inputs['requester']['email'] = $params['email']; | |
unset($params['email']); | |
} | |
//checks for phone | |
if (isset($params['phone'])) { | |
array_push($custom_fields, [ | |
"id" => 11922083929367, //phone | |
"value" => $params['phone'] | |
]); | |
unset($params['phone']); | |
} | |
//checks for eclipseId | |
if (isset($params['eclipseId'])) { | |
array_push($custom_fields, [ | |
"id" => 11922084629527, //eclipseId | |
"value" => $params['eclipseId'] | |
]); | |
unset($params['eclipseId']); | |
} | |
//checks for IP | |
if (isset($params['ip'])) { | |
array_push($custom_fields, [ | |
"id" => 11922084325015, //IP | |
"value" => $params['ip'] | |
]); | |
unset($params['ip']); | |
} | |
//appends other data inputs form the raw email request that is not listed on zendesk | |
if(!empty($params) && (count($params) > 0)) { | |
$inputs['comment']['html_body'] = $inputs['comment']['html_body']." | ".json_encode($params); | |
} | |
$inputs['custom_fields'] = $custom_fields; | |
$ticket = Zendesk::tickets()->create($inputs); | |
return $ticket->ticket->id; | |
} | |
//Improved Version: 2 - Type Hints | |
public function sendRawTypeHinted(array $data) : string { | |
//without documentation, I already know that this function accepts an array, and | |
//will return a string. Also, the moment that I try to use this function incorrectly, | |
//it will throw an exception in the place I incorrectly used it, showing me exactly | |
//where, down to the line, I am passing the incorrect type of data into it. | |
$custom_fields = []; | |
$inputs = [ | |
'subject' => $this->website. ' | Raw Email', | |
'comment' => [ | |
'html_body' => "" | |
], | |
"priority" => "normal", | |
"requester" => [ | |
"email" => "psemailfeed@gmail.com", | |
"name" => "", | |
], | |
'custom_fields' => [] | |
]; | |
$params = $data; | |
//checks for subject | |
if (isset($params['subject'])) { | |
$inputs['subject'] = $this->website. " | ". $params['subject']; | |
unset($params['subject']); | |
} | |
//checks for message content | |
if (isset($params['message'])) { | |
$inputs['comment']['html_body'] = $params['message']; | |
unset($params['message']); | |
} | |
//checks for email | |
if (isset($params['email'])) { | |
array_push($custom_fields, [ | |
"id" => 11949515689239, //IP | |
"value" => $params['email'] | |
]); | |
$inputs['requester']['email'] = $params['email']; | |
unset($params['email']); | |
} | |
//checks for phone | |
if (isset($params['phone'])) { | |
array_push($custom_fields, [ | |
"id" => 11922083929367, //phone | |
"value" => $params['phone'] | |
]); | |
unset($params['phone']); | |
} | |
//checks for eclipseId | |
if (isset($params['eclipseId'])) { | |
array_push($custom_fields, [ | |
"id" => 11922084629527, //eclipseId | |
"value" => $params['eclipseId'] | |
]); | |
unset($params['eclipseId']); | |
} | |
//checks for IP | |
if (isset($params['ip'])) { | |
array_push($custom_fields, [ | |
"id" => 11922084325015, //IP | |
"value" => $params['ip'] | |
]); | |
unset($params['ip']); | |
} | |
//appends other data inputs form the raw email request that is not listed on zendesk | |
if(!empty($params) && (count($params) > 0)) { | |
$inputs['comment']['html_body'] = $inputs['comment']['html_body']." | ".json_encode($params); | |
} | |
$inputs['custom_fields'] = $custom_fields; | |
$ticket = Zendesk::tickets()->create($inputs); | |
return $ticket->ticket->id; | |
} | |
//Improved Version: 3 - x_helpers | |
public function sendRawTypeAgnostic($data) { | |
//Sometimes, a shallow type cast is not enough. Maybe we're expecting a multidimensional | |
//array, and sometimes we get nested objects, or vice-versa. In this case, it's better to | |
//use x_helpers such as xget, xset, and xunset. | |
$custom_fields = []; | |
$inputs = [ | |
'subject' => $this->website. ' | Raw Email', | |
'comment' => [ | |
'html_body' => "" | |
], | |
"priority" => "normal", | |
"requester" => [ | |
"email" => "psemailfeed@gmail.com", | |
"name" => "", | |
], | |
'custom_fields' => [] | |
]; | |
//We know that $custom_fields and $inputs are arrays, because we're creating them here. So, | |
//technically, we don't need to use xget or xset when interacting with them. It's not bad | |
//to do so, it's just not explicitly needed to make this function object/array agnostic. | |
$params = $data; | |
//checks for subject | |
$subject = xget($params, 'subject', null); | |
if($subject !== null) { | |
xset($inputs, 'subject', $this->website. " | " . $subject); | |
xunset($params, 'subject'); | |
} | |
//checks for message content | |
$message = xget($params, 'message', null); | |
if($message !== null) { | |
xset($inputs, 'comment.html_body', $message); | |
xunset($params, 'message'); | |
} | |
//checks for email | |
$email = xget($params, 'email', null); | |
if($email !== null) { | |
$customIdAndValue = [ | |
"id" => 11949515689239, //IP | |
"value" => $email | |
]; | |
$custom_fields[] = $customIdAndValue; | |
xset($inputs, 'requester.email', $email); | |
xunset($params['email']); | |
} | |
//checks for phone | |
$phone = xget($params, 'phone', null); | |
if($phone !== null) { | |
$customIdAndValue = [ | |
"id" => 11922083929367, //phone | |
"value" => $phone | |
]; | |
$custom_fields[] = $customIdAndValue; | |
xunset($params, 'phone'); | |
} | |
//checks for eclipseId | |
$eclipseId = xget($params, 'eclipseId', null); | |
if($eclipseId !== null) { | |
$customIdAndValue = [ | |
"id" => 11922084629527, //eclipseId | |
"value" => $eclipseId | |
]; | |
$custom_fields[] = $customIdAndValue; | |
xunset($params, 'eclipseId'); | |
} | |
//checks for IP | |
$ip = xget($params, 'ip', null); | |
if($ip !== null) { | |
$customIdAndValue = [ | |
"id" => 11922084325015, //IP | |
"value" => $ip | |
]; | |
$custom_fields[] = $customIdAndValue; | |
xunset($params, 'ip'); | |
} | |
//appends other data inputs form the raw email request that is not listed on zendesk | |
if(!empty($params)) { | |
//xmodify is a combined xget and xset operation. | |
//3rd parameter is a closure | |
//The closure accepts 1 parameter: the value at the path, as returned by xget. | |
//The return value of the closure will be passed into xset, to replace the | |
//value at the path. | |
xmodify($inputs, 'comment.html_body', function($htmlBody)use($params) { | |
return $htmlBody . " | " . json_encode($params); | |
}); | |
} | |
xset($inputs, 'custom_fields', $custom_fields); | |
$ticket = Zendesk::tickets()->create($inputs); | |
return $ticket->ticket->id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment