Skip to content

Instantly share code, notes, and snippets.

@avosalmon
Last active May 7, 2019 12:57
Show Gist options
  • Save avosalmon/eb3e8f80e8996228e8a82fe7a258d87c to your computer and use it in GitHub Desktop.
Save avosalmon/eb3e8f80e8996228e8a82fe7a258d87c to your computer and use it in GitHub Desktop.
<?php
class OrderProcessor
{
public function __construct(Slack $slack) {
$this->slack = $slack;
}
public function process(Order $order)
{
if (! $order->getAmount() > 0) {
throw new Exception('Order amount must be more than zero.');
}
if (! $order->isApproved()) {
throw new Exception('Order is not approved.');
}
if (! $order->isCancelled()) {
throw new Exception('Cancelled order cannot be processed.');
}
$order->update();
$message = $this->getMessage($order);
$this->slack->to('#order')->send($message);
}
protected function getMessage(Order $order) : string
{
$format = '@%s Order has been processed. OrderID: %d';
$userName = $order->getUserName();
$message = sprintf($format, $userName, $order->getId());
return $message;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment