62 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. }
  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. }