67 lines
2.2KB

  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. $persist = isset($params['persist']) ? $params['persist'] : true ;
  25. $userMerchant = $userMerchantRepository->findOneBy([
  26. 'user' => $user,
  27. 'merchant' => $merchant
  28. ]) ;
  29. if(!$userMerchant) {
  30. $userMerchant = new $classUserMerchant ;
  31. $userMerchant->setUser($user) ;
  32. $userMerchant->setMerchant($merchant) ;
  33. $userMerchant->setCreditActive(false);
  34. }
  35. $userMerchant->setActive($active) ;
  36. if(isset($params['credit_active'])) {
  37. $userMerchant->setCreditActive($params['credit_active']) ;
  38. }
  39. if(isset($params['credit'])) {
  40. $userMerchant->setCredit($params['credit']) ;
  41. }
  42. if($persist) {
  43. $this->em->persist($userMerchant);
  44. }
  45. if($flush) {
  46. $this->em->flush() ;
  47. }
  48. return $userMerchant ;
  49. }
  50. }