在我的应用程序中有客户和信使。只有当信使目前在线且两位用户来自同一地点时,客户才可以将信息发送给信使。我是否正确使用我的服务与我的实体和映射器。我的商业逻辑是否正确?
当客户要发送递送请求给信使时,我的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
)将在从Controller
通过$request
对象,所以我将创建在Service
每个Item
,并把它们在阵列中,并通过与所述阵列$customer
和$courier
至sendRequest()
方法。这意味着我必须在该方法内执行检查(检查输入数量是否不超过数量等数据库值),是正确的还是不好?
我正在做我的检查/验证正确,并在我的应用程序正确的地方/层?
任何帮助将非常感谢。