src/Service/User/UserService.php line 111

Open in your IDE?
  1. <?php
  2. namespace App\Service\User;
  3. use App\Entity\User;
  4. use App\Entity\UserSettings;
  5. use App\Service\BaseEntityService;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Psr\Container\ContainerInterface;
  8. class UserService extends BaseEntityService
  9. {
  10.     /**
  11.      * @var ContainerInterface
  12.      */
  13.     private $container;
  14.     /**
  15.      * @var \App\Entity\User|null
  16.      */
  17.     private $currentReviewer;
  18.     public function __construct(EntityManagerInterface $emContainerInterface $container)
  19.     {
  20.         parent::__construct($em);
  21.         $this->initialize(User::class);
  22.         $this->container $container;
  23.     }
  24.     public function getOrlovAvUser(): ?User
  25.     {
  26.         return $this->getFirst([
  27.             "firstName" => "Александр",
  28.             "lastName" => "Орлов",
  29.         ]);
  30.     }
  31.     public function getUchitelUser(): ?User
  32.     {
  33.         return $this->getFirst([
  34.             "firstName" => "Учитель",
  35.         ]);
  36.     }
  37.     /**
  38.      * Возвращает набор вариантов для поля User при редактировании TelegramDelayedMessage
  39.      * @param mixed $telegramDelayedMessage
  40.      * @return User[]
  41.      */
  42.     public function getTelegramDelayedMessageChoices($telegramDelayedMessage null): array
  43.     {
  44.         return $this->getBaseService()->getAll();
  45.     }
  46.     /**
  47.      * Возвращает текст метки для выбора User.
  48.      * Поддерживаем сигнатуру с 1 или 2 параметрами, чтобы AbstractBaseType мог вызывать любую из них.
  49.      */
  50.     public function getChoiceLabel(User $user$entity null): string
  51.     {
  52.         return $user->getFirstName() . ($user->getLastName() ? ' ' $user->getLastName() : '');
  53.     }
  54.     /**
  55.      * Возвращает всех кураторов
  56.      * @return User[]
  57.      */
  58.     public function getCurators(): array
  59.     {
  60.         /** @var User[] $users */
  61.         $users $this->getBaseService()->getAll();
  62.         $curators = [];
  63.         foreach ($users as $user) {
  64.             if ($user->getSettings()->isIsCurator()) {
  65.                 $curators[] = $user;
  66.             }
  67.         }
  68.         return $curators;
  69.     }
  70.     public function getReviewers(): array
  71.     {
  72.         /** @var User[] $users */
  73.         $users $this->getBaseService()->getAll();
  74.         $reviewers = [];
  75.         foreach ($users as $user) {
  76.             if ($user->getSettings()->isCanReviewCandidates()) {
  77.                 $reviewers[] = $user;
  78.             }
  79.         }
  80.         return $reviewers;
  81.     }
  82.     /**
  83.      * Возвращает варианты User для поля createdBy при редактировании Payment
  84.      * Вызывается динамически из AbstractBaseType::addEntityField при построении формы PaymentType
  85.      * @param mixed $payment
  86.      * @return User[]
  87.      */
  88.     public function getPaymentChoices($payment null): array
  89.     {
  90.         // По умолчанию возвращаем всех пользователей. При необходимости можно добавить фильтрацию.
  91.         return $this->getBaseService()->getAll();
  92.     }
  93.     public function getLoggedInUser(): ?User
  94.     {
  95.         if (!$this->container->has('security.token_storage')) {
  96.             throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
  97.         }
  98.         if (null === $token $this->container->get('security.token_storage')->getToken()) {
  99.             return null;
  100.         }
  101.         // @deprecated since 5.4, $user will always be a UserInterface instance
  102.         if (!\is_object($user $token->getUser())) {
  103.             // e.g. anonymous authentication
  104.             return null;
  105.         }
  106.         return $user;
  107.     }
  108.     public function getLoggedInUserSettings(): ?UserSettings
  109.     {
  110.         $user $this->getLoggedInUser();
  111.         return $user $user->getSettings() : null;
  112.     }
  113.     public function getAvailableReviewer(): ?User
  114.     {
  115.         $user $this->getLoggedInUser();
  116.         if ($user && $user->getSettings()->isCanReviewCandidates()) {
  117.             return $user;
  118.         }
  119.         $reviewers $this->getReviewers();
  120.         return count($reviewers) > $reviewers[0] : null;
  121.     }
  122.     public function getCurrentReviewer(): ?\App\Entity\User
  123.     {
  124.         if (!$this->currentReviewer) {
  125.             $this->currentReviewer $this->getUchitelUser();
  126.         }
  127.         return $this->currentReviewer;
  128.     }
  129.     public function getStarCurator(): ?User
  130.     {
  131.         return $this->getFirst([
  132.             "firstName" => "*",
  133.         ]);
  134.     }
  135. }