在我的应用程序中有客户和快递员。只有当 Courier 当前在线并且两个用户来自同一位置时,客户才能向 Courier 发送交付请求。
当客户想要向 Courier 发送交付请求时,我的DeliveryRequest
服务有一个sendDeliveryRequest(Request request)
从Controller
.
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
)将在从$request
传递的对象中,所以我将在 中Controller
创建每个并将它们放入数组中,然后将数组与 和 传递给方法。这意味着我必须在该方法中进行检查(检查输入的数量是否不超过数量的数据库值等),这是正确的方法还是不好?Item
Service
$customer
$courier
sendRequest()
我是否在我的应用程序的正确位置/层正确地进行检查/验证?
任何帮助都会非常感谢。