Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

64 lines
2.0KB

  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. $userMerchant->setCreditActive(false);
  33. }
  34. $userMerchant->setActive($active) ;
  35. if(isset($params['credit_active'])) {
  36. $userMerchant->setCreditActive($params['credit_active']) ;
  37. }
  38. if(isset($params['credit'])) {
  39. $userMerchant->setCredit($params['credit']) ;
  40. }
  41. $this->em->persist($userMerchant);
  42. if($flush) {
  43. $this->em->flush() ;
  44. }
  45. return $userMerchant ;
  46. }
  47. }