<?php
namespace Aqarmap\Bundle\DiscussionBundle\Service;
use Aqarmap\Bundle\DiscussionBundle\Constant\DiscussionSearchTabs;
use Aqarmap\Bundle\DiscussionBundle\Constant\DiscussionStatus;
use Aqarmap\Bundle\DiscussionBundle\Entity\Discussion;
use Aqarmap\Bundle\ListingBundle\Entity\Location;
use Aqarmap\Bundle\MainBundle\Adapter\MailerServiceInterface;
use Aqarmap\Bundle\MainBundle\Helpers\MailerHelper;
use Aqarmap\Bundle\SearchBundle\Services\ElasticListingSearch\AbstractElasticListingSearch;
use Aqarmap\Bundle\UserBundle\Entity\User;
use Aqarmap\Bundle\UserBundle\Services\UserInterestManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
class DiscussionManager
{
public const DISCUSSIONS_LIMIT = 3;
private $em;
private $listingSearchService;
private $userInterestsManager;
private $translator;
private $mailer;
private $container;
private $mailerHelper;
public function __construct(
EntityManagerInterface $em,
AbstractElasticListingSearch $listingSearchService,
UserInterestManager $userInterestsManager,
TranslatorInterface $translator,
Environment $templating,
MailerServiceInterface $mailer,
ContainerInterface $container,
MailerHelper $mailerHelper
) {
$this->em = $em;
$this->listingSearchService = $listingSearchService;
$this->userInterestsManager = $userInterestsManager;
$this->container = $container;
$this->translator = $translator;
$this->mailer = $mailer;
$this->mailerHelper = $mailerHelper;
}
/**
* Get Interested Listing from Elastic Search.
*
* @param array $userInterests
*
* @return array $listings
*/
public function getInterestsListings($userInterests = [])
{
$listings = [];
$userInterestsCriteria = $this->userInterestsManager->extractUserInterestsCriteria($userInterests);
foreach ($userInterestsCriteria as $key => $userInterestCriteria) {
$results = $this->listingSearchService->search($userInterestCriteria);
if (\count($results)) {
$listings[$key] = $results;
}
}
return $listings;
}
public function remove(Discussion $discussion): void
{
$discussion->setDeletedAt(new \DateTime());
$this->em->persist($discussion);
$this->em->flush();
}
/**
* @param bool $flush
*/
public function createSubscriber(?User $user, Discussion $discussion, $flush = true): void
{
if (!$user) {
return;
}
$this->unsetSubscriber($user, $discussion);
$discussion->addSubscriber($user);
$this->em->persist($discussion);
$flush ? $this->em->flush() : null;
}
/**
* @param bool $flush
*/
public function unsetSubscriber(User $user, Discussion $discussion, $flush = true): void
{
if ($discussion->getSubscribers()) {
$discussion->removeSubscriber($user);
$this->em->persist($discussion);
$flush ? $this->em->flush() : null;
}
}
public function sendToHelp(Discussion $discussion)
{
$compose = $this->getComposeMessage($discussion);
return $this->mailer->sendMessage($compose);
}
public function concatTitles(array $discussions, int $maxCharacters = 160): string
{
$concatTitle = '';
/** @var Discussion $discussion */
foreach ($discussions as $discussion) {
if (\strlen($concatTitle) > $maxCharacters) {
break;
}
$concatTitle .= $discussion instanceof Discussion ? $discussion->getContent().' ' : ' ';
}
return substr($concatTitle, 0, $maxCharacters);
}
public function getTrendingWithLocation(Location $location, int $limit = self::DISCUSSIONS_LIMIT)
{
$criteria = [
'status' => DiscussionStatus::APPROVED,
'location' => $location,
'tab' => DiscussionSearchTabs::TRENDING,
'sort' => 'commentsCounter',
'limit' => $limit,
];
return $this
->em
->getRepository(Discussion::class)
->filter($criteria);
}
private function getComposeMessage($discussion)
{
$composeMessage = $this->mailerHelper->createMessageWithGlobalAttributes();
$composeMessage->setSubject($this->translator->trans('neighborhoods.help_desk.email.subject'));
$composeMessage->setTo($this->container->getParameter('help_desk_email'));
$composeMessage->setTemplate('@AqarmapDiscussionBundle/Email/discussionHelpDeskEmail.html.twig');
$composeMessage->setTemplateContext(['discussion' => $discussion]);
$compose = $this->mailer->composeMessage($composeMessage);
$compose->getHeaders()->addTextHeader('X-Mail-Category', 'Discussion');
$compose->getHeaders()->addTextHeader('X-Site-Country', $this->container->getParameter('country'));
return $compose;
}
}