No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

MerchantUtils.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use App\Entity\UserMerchant;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\ShopBundle\Context\UserMerchantInterface;
  6. use Symfony\Component\Security\Core\Security;
  7. class MerchantUtils
  8. {
  9. protected $security ;
  10. protected $em ;
  11. public function __construct(Security $security, EntityManagerInterface $em)
  12. {
  13. $this->security = $security ;
  14. $this->em = $em ;
  15. }
  16. public function initUserMerchant($params = [])
  17. {
  18. $classUserMerchant = $this->em->getClassMetadata(UserMerchantInterface::class)->getName() ;
  19. $userMerchantRepository = $this->em->getRepository($classUserMerchant) ;
  20. $user = isset($params['user']) ? $params['user'] : null ;
  21. $merchant = isset($params['merchant']) ? $params['merchant'] : null ;
  22. $active = isset($params['active']) ? $params['active'] : true ;
  23. $flush = isset($params['flush']) ? $params['flush'] : true ;
  24. $userMerchant = $userMerchantRepository->findOneBy([
  25. 'user' => $user,
  26. 'merchant' => $merchant
  27. ]) ;
  28. if(!$userMerchant) {
  29. $userMerchant = new $classUserMerchant ;
  30. $userMerchant->setUser($user) ;
  31. $userMerchant->setMerchant($merchant) ;
  32. }
  33. $userMerchant->setActive($active) ;
  34. if(isset($params['credit_active'])) {
  35. $userMerchant->setCreditActive($params['credit_active']) ;
  36. }else{
  37. $userMerchant->setCreditActive(false);
  38. }
  39. if(isset($params['credit'])) {
  40. $userMerchant->setCredit($params['credit']) ;
  41. }
  42. $this->em->persist($userMerchant);
  43. if($flush) {
  44. $this->em->flush() ;
  45. }
  46. return $userMerchant ;
  47. }
  48. }