【发布时间】:2013-03-18 02:20:53
【问题描述】:
在我的应用程序中有客户和快递员。只有当 Courier 当前在线并且两个用户来自同一位置时,客户才能向 Courier 发送交付请求。
当客户想要向 Courier 发送交付请求时,我的 DeliveryRequest 服务有一个从 Controller 调用的 sendDeliveryRequest(Request request) 方法。
public function sendDeliveryRequest(Request $request) {
$customer = $this->recognitionService->getUser();
$courier = $this->entityFactory->build('Courier');
$courier->setId( $request->post('courierId') );
$courierMapper = $this->mapperFactory->build('Courier');
$courierMapper->fetch($courier);
$deliveryRequest = $this->entityFactory->build('DeliveryRequest');
$someRequestedItems = array();
$deliveryRequest->sendRequest($customer, $courier, $someRequestedItems);
}
到目前为止,在我的 sendRequest(Customer $customer, Courier $courier, Array $items) 方法中,我有:
public function sendRequest(Customer $customer, Courier $courier, Array $items) {
// Check if the couriers account is active
if( !$courier->isActive() ) {
return 'courier not active';
}
// Check if the courier is online
if( !$courier->isOnline() ) {
return 'courier not online';
}
// Check the status of the customers location, active/inactive
if( !$customer->getLocation()->isActive() ) {
return 'customers location disabled';
}
// Check if the customer and the courier live in the same location
if( !$customer->sameLocationAs($courier) ) {
return 'customer and courier in different locations';
}
// More checks
}
到目前为止,对我来说,它看起来不错并且运行良好,但我不能 100% 确定我是否正确地执行了业务逻辑,尤其是 !$customer->sameLocationAs($courier)。
该方法使用提供的 $courier 对象来获取该 Couriers 位置(这是一个带有 id 的对象)并将其与客户位置进行比较,以检查它们是否在同一位置。它工作得很好,但我不确定这是否是完成检查两个用户是否来自同一位置的最佳方法。这是有效的业务逻辑吗?
另外,$deliveryRequest 中的项目,它们的数据(id,quantity)将在从Controller 传递的$request 对象中,所以我将在@987654336 中创建每个Item @ 并将它们放入一个数组中,并将带有$customer 和$courier 的数组传递给sendRequest() 方法。这意味着我必须在该方法中进行检查(检查输入的数量是否不超过数量的数据库值等),这是正确的方法还是不好的方法?
我是否在应用程序的正确位置/层正确地进行检查/验证?
任何帮助将非常感谢。
【问题讨论】:
标签: php oop model-view-controller