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.

69 lines
2.0KB

  1. <?php
  2. namespace common\logic\User\UserProducer\Repository;
  3. use common\logic\AbstractRepository;
  4. use common\logic\Producer\Producer\Model\Producer;
  5. use common\logic\User\User\Model\User;
  6. use common\logic\User\UserProducer\Model\UserProducer;
  7. class UserProducerRepository extends AbstractRepository
  8. {
  9. protected UserProducerRepositoryQuery $query;
  10. public function loadDependencies(): void
  11. {
  12. $this->loadQuery(UserProducerRepositoryQuery::class);
  13. }
  14. public function getDefaultOptionsSearch(): array
  15. {
  16. return [
  17. self::WITH => ['producer'],
  18. self::JOIN_WITH => [],
  19. self::ORDER_BY => '',
  20. self::ATTRIBUTE_ID_PRODUCER => 'user_producer.id_producer'
  21. ];
  22. }
  23. public function findOneUserProducer(User $user)
  24. {
  25. return $this->createDefaultQuery()
  26. ->filterByUser($user)
  27. ->findOne();
  28. }
  29. public function findUserProducersByUser(User $user, bool $active = true, bool $bookmark = true)
  30. {
  31. return $this->createQuery()
  32. ->filterByUser($user)
  33. ->filterByActive($active)
  34. ->filterByBookmark($bookmark)
  35. ->find();
  36. }
  37. public function isProducerBookmarked(User $user): bool
  38. {
  39. $userProducer = $this->findOneUserProducer($user);
  40. return $userProducer ? $userProducer->bookmark : false;
  41. }
  42. public function findUserProducersWithNegativeOrPositiveCredit()
  43. {
  44. return $this->createDefaultQuery()
  45. ->filterHasNegativeOrPositiveCredit()
  46. ->find();
  47. }
  48. public function sumUserProducerCredits()
  49. {
  50. $sumUserProducersCredits = 0;
  51. $userProducersWithNegativeOrPositiveCreditArray = $this->findUserProducersWithNegativeOrPositiveCredit();
  52. foreach ($userProducersWithNegativeOrPositiveCreditArray as $userProducerWithNegativeOrPositiveCredit) {
  53. $sumUserProducersCredits += $userProducerWithNegativeOrPositiveCredit->credit;
  54. }
  55. return $sumUserProducersCredits;
  56. }
  57. }