src/Modules/VoucherCart/Service/CartService.php line 160
<?phpnamespace App\Modules\VoucherCart\Service;use Symfony\Component\HttpFoundation\RequestStack;use Symfony\Contracts\Translation\TranslatorInterface;use Symfony\Component\Form\Form;use App\Modules\VoucherCart\Service\PredefinedVoucherService;class CartService{private $_requestStack;private $_translator;private $_predefinedVoucherService;private $_sessionKey = 'voucher_cart';private $_translationDomain = 'voucherCart';public function __construct(RequestStack $requestStack,TranslatorInterface $translator,PredefinedVoucherService $predefinedVoucherService){$this->_requestStack = $requestStack;$this->_translator = $translator;$this->_predefinedVoucherService = $predefinedVoucherService;}public function getDetails(){$data = $this->_getData();$data['total_price'] = 0;foreach($data['vouchers'] as $key => $voucherData){if ($voucherData['is_predefined']){$predefinedVoucherData = $this->_predefinedVoucherService->getDetails($voucherData['data']['id'], false);$voucherData['name'] = $predefinedVoucherData['package']->getNameInCart();}else$voucherData['name'] = $this->_translator->trans('Voucher kwotowy', [], $this->_translationDomain);$data['vouchers'][$key] = $voucherData;$data['total_price'] += $voucherData['price'] * $voucherData['quantity'];}return $data;}public function isEmpty(){return empty($this->_getData()['vouchers']);}public function addVoucher(Form $form, &$errorMessage = null){$errorMessage = null;$formType = $form->getConfig()->getType()->getInnerType();$voucherData = $form->getData();$cartVoucherData = array('is_predefined' => false,'image_url' => $formType->settingsData['image_url'],'price' => $voucherData['amount'],'quantity' => $voucherData['quantity'],'data' => $voucherData);$cartData = $this->_getData();if (!$this->_validateVoucher($cartData, $cartVoucherData, $formType->settingsData['entity']->getPackage()->getCurrencyCode(), $errorMessage))return false;$cartData['vouchers'][] = $cartVoucherData;$this->_setData($cartData);return true;}public function addPredefinedVoucher(array $voucherData, $quantity = 1, &$errorMessage = null){$errorMessage = null;$cartVoucherData = array('is_predefined' => true,'image_url' => $voucherData['image_url'],'price' => $voucherData['price'],'quantity' => $quantity,'data' => array('id' => $voucherData['id']));$cartData = $this->_getData();if (!$this->_validateVoucher($cartData, $cartVoucherData, $voucherData['package']->getCurrencyCode(), $errorMessage))return false;$cartData['vouchers'][] = $cartVoucherData;$this->_setData($cartData);return true;}private function _validateVoucher(array &$cartData, array &$cartVoucherData, $currencyCode, &$errorMessage){if ($cartData['currency_code'] == '')$cartData['currency_code'] = $currencyCode;elseif ($currencyCode != $cartData['currency_code']){$errorMessage = $this->_translator->trans('Koszyk nie może zawierać voucherów wielowalutowych.', [], $this->_translationDomain);return false;}return true;}public function updateCartFromForm(Form $form){$formType = $form->getConfig()->getType()->getInnerType();$cartData = $this->_getData();foreach(array_keys($cartData['vouchers']) as $cartVoucherIndex){if (!in_array($cartVoucherIndex, array_keys($form['vouchers']->getData())))unset($cartData['vouchers'][$cartVoucherIndex]);}foreach($form['vouchers'] as $index => $voucherForm){if (!isset($cartData['vouchers'][$index]))continue;$cartData['vouchers'][$index]['quantity'] = max($voucherForm['quantity']->getData(), 1);}if (!$cartData['vouchers'])$cartData['currency_code'] = null;$cartData['payment_method'] = $form['paymentMethod']->getData();$this->_setData($cartData);$formType->cartData = $this->getDetails();}public function clear(){$this->_setData(null);}private function _getData(){$data = $this->_requestStack->getSession()->get($this->_sessionKey);if (!$data){$data = array('vouchers' => array(),'currency_code' => null,'payment_method' => null);}return $data;}private function _setData($data){$this->_requestStack->getSession()->set($this->_sessionKey, $data);}}