src/Component/CompanyMatch/EventSubscriber/CompanyMatchUserSubscriber.php line 40

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Component\CompanyMatch\EventSubscriber;
  4. use App\Component\CompanyMatch\ApproachStrategy;
  5. use App\Component\CompanyMatch\Calculator\ApproachCalculator;
  6. use App\Component\CompanyMatch\Event\CompanyMatchUserEvent;
  7. use App\Component\CompanyMatch\Message\InvalidateCompanyMatchUserCompanies;
  8. use App\Component\CompanyMatch\Message\MailCompaniesToApproach;
  9. use App\Component\CompanyMatch\Util\CompanyMatchUserHistorian;
  10. use App\Entity\Company;
  11. use App\Entity\CompanyMatchUser;
  12. use App\Entity\SiteUserData;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\Common\Collections\Collection;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Doctrine\ORM\PersistentCollection;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\Messenger\MessageBusInterface;
  19. class CompanyMatchUserSubscriber implements EventSubscriberInterface
  20. {
  21.     public function __construct(
  22.         protected readonly ApproachCalculator $approachCalculator,
  23.         protected readonly MessageBusInterface $messageBus,
  24.         protected readonly CompanyMatchUserHistorian $companyMatchUserHistorian,
  25.         protected readonly EntityManagerInterface $entityManager,
  26.     ) {
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             CompanyMatchUserEvent::class => 'preUpdate',
  32.         ];
  33.     }
  34.     public function preUpdate(CompanyMatchUserEvent $event): void
  35.     {
  36.         $this->checkCompanies($event);
  37.     }
  38.     private function checkCompanies(CompanyMatchUserEvent $event): void
  39.     {
  40.         $siteUserData $event->getUser();
  41.         if (!\is_int($siteUserData->getId())) {
  42.             return;
  43.         }
  44.         $companyCollection $this->approachCalculator->calculateApproachableCompaniesCollection($siteUserData);
  45.         $this->messageBus->dispatch(new MailCompaniesToApproach($companyCollection$siteUserData->getId()));
  46.         $removableCompanies $this->calculateRemoveableCompanies($siteUserData);
  47.         if (!== \count($removableCompanies)) {
  48.             $this->messageBus->dispatch(new InvalidateCompanyMatchUserCompanies($removableCompanies$siteUserData->getId()));
  49.         }
  50.         $this->entityManager->persist($siteUserData);
  51.         $this->entityManager->flush();
  52.     }
  53.     /**
  54.      * @return Collection<int, Company>
  55.      */
  56.     private function calculateRemoveableCompanies(SiteUserData $siteUserData): Collection
  57.     {
  58.         $relatedCompaniesMatches $this->companyMatchUserHistorian->findHistoryForUser($siteUserData);
  59.         $companies $relatedCompaniesMatches->map(fn (CompanyMatchUser $companyMatchUser) => $companyMatchUser->getCompany());
  60.         if (ApproachStrategy::NotApproachable === $siteUserData->getApproachStrategy()) {
  61.             return $companies;
  62.         }
  63.         if (ApproachStrategy::SpecificallyApproachable === $siteUserData->getApproachStrategy()) {
  64.             if (!$siteUserData->hasCompaniesAllowedToApproach()) {
  65.                 return $companies;
  66.             }
  67.             /** @var PersistentCollection<int, Company> $collection */
  68.             $collection $siteUserData->getCompaniesAllowedToApproach();
  69.             /** @var Company[] $deleteDiff */
  70.             $deleteDiff $collection->getDeleteDiff();
  71.             return new ArrayCollection($deleteDiff);
  72.         }
  73.         return new ArrayCollection();
  74.     }
  75. }