<?php
namespace App\Extensions;
use App\Constant\FileTypeConstant;
use App\Constant\UtilsConstant;
use App\Entity\Band;
use App\Entity\BandMember;
use App\Entity\File;
use App\Entity\Order;
use App\Entity\PremiumSubscription;
use App\Entity\User;
use App\Services\PremiumManager;
use DateTime;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class UtilsExtension extends AbstractExtension
{
private ?User $loggedUser = null;
private ?Request $request;
private TranslatorInterface $translator;
private EntityManagerInterface $entityManager;
private PremiumManager $premiumManager;
public function __construct(
TokenStorageInterface $tokenStorage,
RequestStack $requestStack,
EntityManagerInterface $entityManager,
PremiumManager $premiumManager,
TranslatorInterface $translator
) {
if ($tokenStorage->getToken() && $tokenStorage->getToken()->getUser() instanceof User) {
$this->loggedUser = $tokenStorage->getToken()->getUser();
}
$this->request = $requestStack->getCurrentRequest();
$this->translator = $translator;
$this->entityManager = $entityManager;
$this->premiumManager = $premiumManager;
}
public function getFunctions(): array
{
return [
new TwigFunction('get_time_elapsed_string', [$this, 'timeElapsedString']),
new TwigFunction('current_user_is_in_band', [$this, 'currentUserIsInBand']),
new TwigFunction('band_has_subscription', [$this, 'bandHasSubscription']),
new TwigFunction('get_user_music_played', [$this, 'getUserMusicPlayed']),
new TwigFunction('current_user_is_following_band', [$this, 'currentUserIsFollowingBand']),
new TwigFunction('current_user_is_following_user', [$this, 'currentUserIsFollowingUser']),
new TwigFunction('get_real_redirect_url_on_load', [$this, 'getRealRedirectUrlOnLoad']),
new TwigFunction('is_ajax_request', [$this, 'isAjaxRequest']),
new TwigFunction('get_user_by_email', [$this, 'getUserByEmail']),
];
}
public function isAjaxRequest(): bool {
return $this->request->isXmlHttpRequest();
}
public function getUserByEmail(string $email): ?User {
return $this->entityManager
->getRepository(User::class)
->findOneBy(array('email' => $email));
}
public function getUserMusicPlayed(User $user, $limit = 0): array {
return $this->entityManager
->getRepository(File::class)
->getUserLastPublicMusics($user, $limit);
}
public function getRealRedirectUrlOnLoad(): ?string {
if ($this->request->isXmlHttpRequest()) {
return null;
}
$realRedirectUrl = $this->request->getSession()->get(UtilsConstant::REAL_REDIRECT_URL);
if ($realRedirectUrl) {
$this->request->getSession()->remove(UtilsConstant::REAL_REDIRECT_URL);
}
return $realRedirectUrl;
}
public function currentUserIsInBand(Band $band): bool
{
return $this->loggedUser && $this->loggedUser->isInBand($band);
}
public function bandHasSubscription(Band $band): bool
{
return $band->getCurrentOrder() instanceof Order;
}
public function currentUserIsFollowingBand(Band $band): bool
{
return
$band &&
$this->loggedUser &&
!!count($band->getSubscribers()->filter(function ($followAction) {
return $followAction->getUser() === $this->loggedUser;
}));
}
public function currentUserIsFollowingUser(User $user): bool
{
if (!($this->loggedUser instanceof User)) {
return false;
}
return !!count($user->getSubscribers()->filter(function ($followAction) {
return $followAction->getUser() === $this->loggedUser;
}));
}
public function timeElapsedString($ago, $onlyFirstElement = true): string
{
$now = new DateTime();
$diff = $now->diff($ago);
$string = [
'y' => 'common.date.year',
'm' => 'common.date.month',
'd' => 'common.date.day',
'h' => 'common.date.hour',
'i' => 'common.date.minute',
];
foreach ($string as $k => &$v) {
if ($diff->$k === 0) {
unset($string[$k]);
continue;
}
$v = $this->translator->trans($v, ['count' => $diff->$k]);
}
if ($onlyFirstElement) {
$string = array_slice($string, 0, 1);
}
return count($string) > 0 ?
$this->translator->trans('common.date.ago', ['value' => implode(', ', $string)]):
$this->translator->trans('common.date.just_now');
}
public function getName(): string
{
return 'utils_twig_extension';
}
}