em = $em; $this->paymentModeManager = $paymentMode; $this->paymentGroupeRepository = $this->em->getRepository(PaymentGroup::class); } /** * @param PaymentGroup $_element * @return bool|null */ public function quickUpdate(PaymentGroup $_element) { if (!$this->em->contains($_element)) { try { $this->em->persist($_element); } catch (ORMException $e) { return null; } } try { $this->em->flush(); } catch (OptimisticLockException $e) { return null; } catch (ORMException $e) { return null; } return true; } /** * @param PaymentGroup $element * @return bool */ private function delete(PaymentGroup $element){ if($this->em->contains($element)){ try { $this->em->remove($element); $this->em->flush(); } catch (ORMException $e) { return false; } } return true; } /** * @param string $_publicKey * @return object|PaymentGroup|null */ public function getByPublicKey(string $_publicKey){ return $this->paymentGroupeRepository->findOneBy(['publicKey'=>$_publicKey]); } /** * @return array|object[]|PaymentGroup[] */ public function getAll(){ return $this->paymentGroupeRepository->findBy(['deleted'=>false]); } public function getByGroupName(string $_groupName){ return $this->paymentGroupeRepository->findOneBy(['groupName'=>$_groupName]); } /** * @param string $_name * @param PaymentMode $_paymentMode * @return PaymentGroup|null */ public function createGroup(string $_name, PaymentMode $_paymentMode){ $element = new PaymentGroup(); $element->setGroupName($_name); $element->addPaymentsMode($_paymentMode); if (!$this->quickUpdate($element)) return null; $_paymentMode->addGroup($element); $this->paymentModeManager->quickUpdate($_paymentMode); return $element; } public function deleteGroup(string $_publicKey){ $group = $this->getByPublicKey($_publicKey); if (!$this->delete($group)) return null; return true; } public function addGroupToPaymentMode(PaymentMode $_paymentMode, array $_data){ // Delete the first line named _csrf_token from $data array unset($_data['_csrf_token']); // Recuperate paymentGroup and add it to paymentMode foreach ($_data as $publicKey){ $group = $this->getByPublicKey($publicKey); $_paymentMode->addGroup($group); $this->paymentModeManager->quickUpdate($_paymentMode); } return $_paymentMode; } public function updatePaymentGroup(PaymentGroup $_paymentGroup, string $_name){ if ((!$_paymentGroup instanceof PaymentGroup) || empty($_name)) return null; $_paymentGroup->setGroupName($_name); if (!$this->quickUpdate($_paymentGroup)) return null; return $_paymentGroup; } }