Last active
May 7, 2019 12:57
-
-
Save avosalmon/eb3e8f80e8996228e8a82fe7a258d87c 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 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