您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PointSaleUtils.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\ShopBundle\Context\PointSaleInterface;
  5. use Lc\ShopBundle\Context\UserInterface;
  6. use Lc\ShopBundle\Context\UserPointSaleInterface;
  7. class PointSaleUtils
  8. {
  9. protected $em ;
  10. public function __construct(EntityManagerInterface $em)
  11. {
  12. $this->em = $em ;
  13. }
  14. public function isUserLinkedToPointSale(UserInterface $user, PointSaleInterface $pointSale)
  15. {
  16. foreach($user->getUserPointSales() as $userPointSale) {
  17. if($userPointSale->getPointSale()->getId() == $pointSale->getId()) {
  18. return true ;
  19. }
  20. }
  21. return false ;
  22. }
  23. public function linkUserToPointSale(UserInterface $user, PointSaleInterface $pointSale)
  24. {
  25. if(!$this->isUserLinkedToPointSale($user, $pointSale)) {
  26. $userPointSaleClass = $this->em->getClassMetadata(UserPointSaleInterface::class)->getName();
  27. $userPointSale = new $userPointSaleClass ;
  28. $userPointSale->setUser($user) ;
  29. $userPointSale->setPointSale($pointSale) ;
  30. $this->em->persist($userPointSale);
  31. $this->em->flush() ;
  32. }
  33. }
  34. }