Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MerchantUtils.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. }
  37. if(isset($params['credit'])) {
  38. $userMerchant->setCredit($params['credit']) ;
  39. }
  40. $this->em->persist($userMerchant);
  41. if($flush) {
  42. $this->em->flush() ;
  43. }
  44. return $userMerchant ;
  45. }
  46. }