src/Service/User/UserService.php line 123

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.     /**
  94.      * Варианты пользователей для поля Candidate::curator.
  95.      * Вызывается динамически из AbstractBaseType::addEntityField как getCandidateChoices
  96.      * @param mixed $candidate
  97.      * @return User[]
  98.      */
  99.     public function getCandidateChoices($candidate null): array
  100.     {
  101.         // Используем список кураторов — это логичный набор для выбора кураторов кандидата.
  102.         return $this->getCurators();
  103.     }
  104.     public function getLoggedInUser(): ?User
  105.     {
  106.         if (!$this->container->has('security.token_storage')) {
  107.             throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
  108.         }
  109.         if (null === $token $this->container->get('security.token_storage')->getToken()) {
  110.             return null;
  111.         }
  112.         // @deprecated since 5.4, $user will always be a UserInterface instance
  113.         if (!\is_object($user $token->getUser())) {
  114.             // e.g. anonymous authentication
  115.             return null;
  116.         }
  117.         return $user;
  118.     }
  119.     public function getLoggedInUserSettings(): ?UserSettings
  120.     {
  121.         $user $this->getLoggedInUser();
  122.         return $user $user->getSettings() : null;
  123.     }
  124.     public function getAvailableReviewer(): ?User
  125.     {
  126.         $user $this->getLoggedInUser();
  127.         if ($user && $user->getSettings()->isCanReviewCandidates()) {
  128.             return $user;
  129.         }
  130.         $reviewers $this->getReviewers();
  131.         return count($reviewers) > $reviewers[0] : null;
  132.     }
  133.     public function getCurrentReviewer(): ?\App\Entity\User
  134.     {
  135.         if (!$this->currentReviewer) {
  136.             $this->currentReviewer $this->getUchitelUser();
  137.         }
  138.         return $this->currentReviewer;
  139.     }
  140.     public function getStarCurator(): ?User
  141.     {
  142.         return $this->getFirst([
  143.             "firstName" => "*",
  144.         ]);
  145.     }
  146. }