You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.7KB

  1. <?php
  2. namespace common\logic\PointSale\UserPointSale\Service;
  3. use common\logic\AbstractBuilder;
  4. use common\logic\PointSale\PointSale\Model\PointSale;
  5. use common\logic\PointSale\UserPointSale\Model\UserPointSale;
  6. use common\logic\PointSale\UserPointSale\Repository\UserPointSaleRepository;
  7. use common\logic\User\User\Model\User;
  8. class UserPointSaleBuilder extends AbstractBuilder
  9. {
  10. protected UserPointSaleRepository $userPointSaleRepository;
  11. public function loadDependencies(): void
  12. {
  13. $this->userPointSaleRepository = $this->loadService(UserPointSaleRepository::class);
  14. }
  15. public function instanciateUserPointSale(User $user, PointSale $pointSale, string $comment = null): UserPointSale
  16. {
  17. $userPointSale = new UserPointSale();
  18. $userPointSale->populatePointSale($pointSale);
  19. $userPointSale->populateUser($user);
  20. if($comment) {
  21. $userPointSale->comment = $comment;
  22. }
  23. return $userPointSale;
  24. }
  25. public function createUserPointSale(User $user, PointSale $pointSale, string $comment = null): UserPointSale
  26. {
  27. $userPointSale = $this->instanciateUserPointSale($user, $pointSale, $comment);
  28. $this->saveCreate($userPointSale);
  29. return $userPointSale;
  30. }
  31. public function createUserPointSaleIfNotExist(User $user, PointSale $pointSale, string $comment = null): UserPointSale
  32. {
  33. return $this->userPointSaleRepository->findOneUserPointSale($user, $pointSale)
  34. ?? $this->createUserPointSale($user, $pointSale);
  35. }
  36. public function deleteUserPointSalesByPointSale(PointSale $pointSale): void
  37. {
  38. UserPointSale::deleteAll(['id_point_sale' => $pointSale->id]);
  39. }
  40. }