src/Decorator/ApplicantFormRetriever.php line 25

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Decorator;
  4. use App\Component\Configuration\Util\Config;
  5. use App\Entity\ApplicantForm;
  6. use App\Entity\Option;
  7. use App\Entity\OptionValue;
  8. use App\Entity\Vacancy;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use MobileDetectBundle\DeviceDetector\MobileDetector;
  12. class ApplicantFormRetriever implements ApplicantFormRetrieverInterface
  13. {
  14.     public function __construct(
  15.         private readonly MobileDetector $mobileDetector,
  16.         private readonly Config $config,
  17.         private readonly EntityManagerInterface $manager
  18.     ) {
  19.     }
  20.     public function retrieve(Vacancy $vacancy): ApplicantForm
  21.     {
  22.         /** @var ApplicantForm|null $mobileForm */
  23.         $mobileForm $this->config->get('site_vacancy_mobile_application_form');
  24.         if ($this->mobileDetector->isMobile() && null !== $mobileForm) {
  25.             return $mobileForm;
  26.         }
  27.         $customApplicantForm $vacancy->getCustomApplicantForm();
  28.         if ((bool) $vacancy->isApplicantFormCustom() && null !== $customApplicantForm) {
  29.             return $customApplicantForm;
  30.         }
  31.         $applicantForm $vacancy->getApplicantForm();
  32.         if (null !== $applicantForm) {
  33.             return $applicantForm;
  34.         }
  35.         $applicantForm $this->findApplicantFormFromOptionValues($vacancy);
  36.         if (null !== $applicantForm) {
  37.             return $applicantForm;
  38.         }
  39.         /** @var ApplicantForm $applicantForm */
  40.         $applicantForm $this->manager->getRepository(ApplicantForm::class)->findOneBy(['default' => true]);
  41.         return $applicantForm;
  42.     }
  43.     private function findApplicantFormFromOptionValues(Vacancy $vacancy): ?ApplicantForm
  44.     {
  45.         /** @var bool $formStrategy */
  46.         $formStrategy $this->config->get('site_vacancy_applicant_form_strategy');
  47.         if (!$formStrategy) {
  48.             return null;
  49.         }
  50.         $formOption $this->config->get('site_vacancy_applicant_form_option');
  51.         if (!$formOption instanceof Option) {
  52.             return null;
  53.         }
  54.         /** @var Collection<1, OptionValue> $optionValues */
  55.         $optionValues $vacancy->getOptionValues();
  56.         if ($optionValues->isEmpty()) {
  57.             return null;
  58.         }
  59.         $filteredSearchFilters $optionValues->filter(function (OptionValue $optionValue) use ($formOption) {
  60.             /** @var Option $formOption */
  61.             if ($optionValue->getOption()?->getId() !== $formOption->getId()) {
  62.                 return false;
  63.             }
  64.             return null !== $optionValue->getApplicantForm();
  65.         });
  66.         if ($filteredSearchFilters->isEmpty()) {
  67.             return null;
  68.         }
  69.         /** @var OptionValue $optionValue */
  70.         $optionValue $filteredSearchFilters->first();
  71.         return $optionValue->getApplicantForm();
  72.     }
  73. }