Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

50 lines
1.6KB

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