src/Aqarmap/Bundle/DiscussionBundle/Service/DiscussionManager.php line 57

Open in your IDE?
  1. <?php
  2. namespace Aqarmap\Bundle\DiscussionBundle\Service;
  3. use Aqarmap\Bundle\DiscussionBundle\Constant\DiscussionSearchTabs;
  4. use Aqarmap\Bundle\DiscussionBundle\Constant\DiscussionStatus;
  5. use Aqarmap\Bundle\DiscussionBundle\Entity\Discussion;
  6. use Aqarmap\Bundle\ListingBundle\Entity\Location;
  7. use Aqarmap\Bundle\MainBundle\Adapter\MailerServiceInterface;
  8. use Aqarmap\Bundle\MainBundle\Helpers\MailerHelper;
  9. use Aqarmap\Bundle\SearchBundle\Services\ElasticListingSearch\AbstractElasticListingSearch;
  10. use Aqarmap\Bundle\UserBundle\Entity\User;
  11. use Aqarmap\Bundle\UserBundle\Services\UserInterestManager;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use Twig\Environment;
  16. class DiscussionManager
  17. {
  18.     public const DISCUSSIONS_LIMIT 3;
  19.     private $em;
  20.     private $listingSearchService;
  21.     private $userInterestsManager;
  22.     private $translator;
  23.     private $mailer;
  24.     private $container;
  25.     private $mailerHelper;
  26.     public function __construct(
  27.         EntityManagerInterface $em,
  28.         AbstractElasticListingSearch $listingSearchService,
  29.         UserInterestManager $userInterestsManager,
  30.         TranslatorInterface $translator,
  31.         Environment $templating,
  32.         MailerServiceInterface $mailer,
  33.         ContainerInterface $container,
  34.         MailerHelper $mailerHelper
  35.     ) {
  36.         $this->em $em;
  37.         $this->listingSearchService $listingSearchService;
  38.         $this->userInterestsManager $userInterestsManager;
  39.         $this->container $container;
  40.         $this->translator $translator;
  41.         $this->mailer $mailer;
  42.         $this->mailerHelper $mailerHelper;
  43.     }
  44.     /**
  45.      * Get Interested Listing from Elastic Search.
  46.      *
  47.      * @param array $userInterests
  48.      *
  49.      * @return array $listings
  50.      */
  51.     public function getInterestsListings($userInterests = [])
  52.     {
  53.         $listings = [];
  54.         $userInterestsCriteria $this->userInterestsManager->extractUserInterestsCriteria($userInterests);
  55.         foreach ($userInterestsCriteria as $key => $userInterestCriteria) {
  56.             $results $this->listingSearchService->search($userInterestCriteria);
  57.             if (\count($results)) {
  58.                 $listings[$key] = $results;
  59.             }
  60.         }
  61.         return $listings;
  62.     }
  63.     public function remove(Discussion $discussion): void
  64.     {
  65.         $discussion->setDeletedAt(new \DateTime());
  66.         $this->em->persist($discussion);
  67.         $this->em->flush();
  68.     }
  69.     /**
  70.      * @param bool $flush
  71.      */
  72.     public function createSubscriber(?User $userDiscussion $discussion$flush true): void
  73.     {
  74.         if (!$user) {
  75.             return;
  76.         }
  77.         $this->unsetSubscriber($user$discussion);
  78.         $discussion->addSubscriber($user);
  79.         $this->em->persist($discussion);
  80.         $flush $this->em->flush() : null;
  81.     }
  82.     /**
  83.      * @param bool $flush
  84.      */
  85.     public function unsetSubscriber(User $userDiscussion $discussion$flush true): void
  86.     {
  87.         if ($discussion->getSubscribers()) {
  88.             $discussion->removeSubscriber($user);
  89.             $this->em->persist($discussion);
  90.             $flush $this->em->flush() : null;
  91.         }
  92.     }
  93.     public function sendToHelp(Discussion $discussion)
  94.     {
  95.         $compose $this->getComposeMessage($discussion);
  96.         return $this->mailer->sendMessage($compose);
  97.     }
  98.     public function concatTitles(array $discussionsint $maxCharacters 160): string
  99.     {
  100.         $concatTitle '';
  101.         /** @var Discussion $discussion */
  102.         foreach ($discussions as $discussion) {
  103.             if (\strlen($concatTitle) > $maxCharacters) {
  104.                 break;
  105.             }
  106.             $concatTitle .= $discussion instanceof Discussion $discussion->getContent().' ' ' ';
  107.         }
  108.         return substr($concatTitle0$maxCharacters);
  109.     }
  110.     public function getTrendingWithLocation(Location $locationint $limit self::DISCUSSIONS_LIMIT)
  111.     {
  112.         $criteria = [
  113.             'status' => DiscussionStatus::APPROVED,
  114.             'location' => $location,
  115.             'tab' => DiscussionSearchTabs::TRENDING,
  116.             'sort' => 'commentsCounter',
  117.             'limit' => $limit,
  118.         ];
  119.         return $this
  120.             ->em
  121.             ->getRepository(Discussion::class)
  122.             ->filter($criteria);
  123.     }
  124.     private function getComposeMessage($discussion)
  125.     {
  126.         $composeMessage $this->mailerHelper->createMessageWithGlobalAttributes();
  127.         $composeMessage->setSubject($this->translator->trans('neighborhoods.help_desk.email.subject'));
  128.         $composeMessage->setTo($this->container->getParameter('help_desk_email'));
  129.         $composeMessage->setTemplate('@AqarmapDiscussionBundle/Email/discussionHelpDeskEmail.html.twig');
  130.         $composeMessage->setTemplateContext(['discussion' => $discussion]);
  131.         $compose $this->mailer->composeMessage($composeMessage);
  132.         $compose->getHeaders()->addTextHeader('X-Mail-Category''Discussion');
  133.         $compose->getHeaders()->addTextHeader('X-Site-Country'$this->container->getParameter('country'));
  134.         return $compose;
  135.     }
  136. }