У меня есть несколько сложный механизм ценообразования в моем приложении - Вот некоторые из моих бизнес-правил, чтобы установить сцену (сущности выделены жирным):
Прямо сейчас у меня есть EntityRepository для Price Points, чтобы по существу определить правильную цену для базового продукта. То же самое относится к Уникальному дополнению и Опциям.
public function getThePrice($Product, $qty, $Website, $Customer = null)
{
//all logic to get product price for this given instance goes here. Good.
}
public function indexAction()
{
$Product = $em->dostuffwithpostdata;
$qty = POST['qty']; //inb4insecure trolls
$Website = $em->dostuff();
$Customer = (if user is logged in, return their object with $em, otherwise null as it is a guest or public person); // No business logic here, just understanding the request.
$price = $em->getRepository(PricePointRepository)->getThePrice($Product,$qty,Website,$Customer);
$Options[] = $em->dostuffwithPOSTdata;
$optionsPrice = 0;
//Below is some logic directly related to pricing the product.
foreach($Options as $option) {
if($option->hasRule()) {
$optionsPrice += $ruleprice; //after some other stuff of course)
} else {
$optionsPrice += $em->getRepository(OptionPricePoints)->getPrice($option->getID(),$qty);
}
}
$uniqueAdditionPrice = $em->stuff;
$finalprice = $price + $optionsPrice + $uniqueAdditionPrice; //This is logic related to how I price this type of product!
$unitprice = $finalprice / $qty;
//twig stuff to render and show $finalprice, $unitprice, $uniqueAdditionPrice
}
Это только для страницы товара. Что произойдет, когда я дойду до корзины, сохранения заказа и т.д., когда эта логика должна быть использована повторно. Как вы можете видеть, я использую Doctrine для извлечения данных на основе моей бизнес-логики в классах репозитория.
Я с радостью приму ответы urdoingitwrong, потому что я действительно считаю, что это неправильно. Как мне это исправить? Нечто прекрасное было бы в виде сервиса, который по сути выглядит так:
$pricer = getPricerService->Pricer($Entities,$postdata,$etc);
$unitPrice = $pricer->getUnitPrice();
$totalPrice = $pricer->getTotalPrice();
$optionsPrice = $pricer->getOptionsPrice();
Но я понятия не имею, как это сделать в Symfony/Doctrine, особенно с учетом того, как Doctrine и репозитории доступны в контроллерах.