Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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