src/Extensions/UtilsExtension.php line 84

Open in your IDE?
  1. <?php
  2. namespace App\Extensions;
  3. use App\Constant\FileTypeConstant;
  4. use App\Constant\UtilsConstant;
  5. use App\Entity\Band;
  6. use App\Entity\BandMember;
  7. use App\Entity\File;
  8. use App\Entity\Order;
  9. use App\Entity\PremiumSubscription;
  10. use App\Entity\User;
  11. use App\Services\PremiumManager;
  12. use DateTime;
  13. use Doctrine\ORM\EntityManager;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use Twig\Extension\AbstractExtension;
  20. use Twig\TwigFunction;
  21. class UtilsExtension extends AbstractExtension
  22. {
  23.     private ?User $loggedUser null;
  24.     private ?Request $request;
  25.     private TranslatorInterface $translator;
  26.     private EntityManagerInterface $entityManager;
  27.     private PremiumManager $premiumManager;
  28.     public function __construct(
  29.         TokenStorageInterface $tokenStorage,
  30.         RequestStack $requestStack,
  31.         EntityManagerInterface $entityManager,
  32.         PremiumManager $premiumManager,
  33.         TranslatorInterface $translator
  34.     ) {
  35.         if ($tokenStorage->getToken() && $tokenStorage->getToken()->getUser() instanceof User) {
  36.             $this->loggedUser $tokenStorage->getToken()->getUser();
  37.         }
  38.         $this->request $requestStack->getCurrentRequest();
  39.         $this->translator $translator;
  40.         $this->entityManager $entityManager;
  41.         $this->premiumManager $premiumManager;
  42.     }
  43.     public function getFunctions(): array
  44.     {
  45.         return [
  46.             new TwigFunction('get_time_elapsed_string', [$this'timeElapsedString']),
  47.             new TwigFunction('current_user_is_in_band', [$this'currentUserIsInBand']),
  48.             new TwigFunction('band_has_subscription', [$this'bandHasSubscription']),
  49.             new TwigFunction('get_user_music_played', [$this'getUserMusicPlayed']),
  50.             new TwigFunction('current_user_is_following_band', [$this'currentUserIsFollowingBand']),
  51.             new TwigFunction('current_user_is_following_user', [$this'currentUserIsFollowingUser']),
  52.             new TwigFunction('get_real_redirect_url_on_load', [$this'getRealRedirectUrlOnLoad']),
  53.             new TwigFunction('is_ajax_request', [$this'isAjaxRequest']),
  54.             new TwigFunction('get_user_by_email', [$this'getUserByEmail']),
  55.         ];
  56.     }
  57.     public function isAjaxRequest(): bool {
  58.         return $this->request->isXmlHttpRequest();
  59.     }
  60.     public function getUserByEmail(string $email): ?User {
  61.         return $this->entityManager
  62.             ->getRepository(User::class)
  63.             ->findOneBy(array('email' => $email));
  64.     }
  65.     public function getUserMusicPlayed(User $user$limit 0): array {
  66.         return $this->entityManager
  67.             ->getRepository(File::class)
  68.             ->getUserLastPublicMusics($user$limit);
  69.     }
  70.     public function getRealRedirectUrlOnLoad(): ?string {
  71.         if ($this->request->isXmlHttpRequest()) {
  72.             return null;
  73.         }
  74.         $realRedirectUrl $this->request->getSession()->get(UtilsConstant::REAL_REDIRECT_URL);
  75.         if ($realRedirectUrl) {
  76.             $this->request->getSession()->remove(UtilsConstant::REAL_REDIRECT_URL);
  77.         }
  78.         return $realRedirectUrl;
  79.     }
  80.     public function currentUserIsInBand(Band $band): bool
  81.     {
  82.         return $this->loggedUser && $this->loggedUser->isInBand($band);
  83.     }
  84.     public function bandHasSubscription(Band $band): bool
  85.     {
  86.         return $band->getCurrentOrder() instanceof Order;
  87.     }
  88.     public function currentUserIsFollowingBand(Band $band): bool
  89.     {
  90.         return
  91.             $band &&
  92.             $this->loggedUser &&
  93.             !!count($band->getSubscribers()->filter(function ($followAction) {
  94.                 return $followAction->getUser() === $this->loggedUser;
  95.             }));
  96.     }
  97.     public function currentUserIsFollowingUser(User $user): bool
  98.     {
  99.         if (!($this->loggedUser instanceof User)) {
  100.             return false;
  101.         }
  102.         return !!count($user->getSubscribers()->filter(function ($followAction) {
  103.             return $followAction->getUser() === $this->loggedUser;
  104.         }));
  105.     }
  106.     public function timeElapsedString($ago$onlyFirstElement true): string
  107.     {
  108.         $now = new DateTime();
  109.         $diff $now->diff($ago);
  110.         $string = [
  111.             'y' => 'common.date.year',
  112.             'm' => 'common.date.month',
  113.             'd' => 'common.date.day',
  114.             'h' => 'common.date.hour',
  115.             'i' => 'common.date.minute',
  116.         ];
  117.         foreach ($string as $k => &$v) {
  118.             if ($diff->$k === 0) {
  119.                 unset($string[$k]);
  120.                 continue;
  121.             }
  122.             $v $this->translator->trans($v, ['count' => $diff->$k]);
  123.         }
  124.         if ($onlyFirstElement) {
  125.             $string array_slice($string01);
  126.         }
  127.         return count($string) > ?
  128.             $this->translator->trans('common.date.ago', ['value' => implode(', '$string)]):
  129.             $this->translator->trans('common.date.just_now');
  130.     }
  131.     public function getName(): string
  132.     {
  133.         return 'utils_twig_extension';
  134.     }
  135. }