diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonController.php b/src/Bundle/ChillPersonBundle/Controller/PersonController.php index a42cfef7b..c8bdf9906 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonController.php @@ -71,13 +71,13 @@ final class PersonController extends AbstractController */ private $logger; + private PersonACLAwareRepositoryInterface $personACLAwareRepository; + /** * @var ValidatorInterface */ private $validator; - private PersonACLAwareRepositoryInterface $personACLAwareRepository; - public function __construct( TranslatorInterface $translator, EventDispatcherInterface $eventDispatcher, diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php b/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php index e91732269..ee5c7e929 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonDuplicateController.php @@ -39,6 +39,11 @@ class PersonDuplicateController extends Controller */ private $eventDispatcher; + /** + * @var PersonACLAwareRepositoryInterface + */ + private $personACLAwareRepository; + /** * @var \Chill\PersonBundle\Actions\Remove\PersonMove */ @@ -49,11 +54,6 @@ class PersonDuplicateController extends Controller */ private $personRepository; - /** - * @var PersonACLAwareRepositoryInterface - */ - private $personACLAwareRepository; - /** * @var \Symfony\Component\Translation\TranslatorInterface */ @@ -246,7 +246,6 @@ class PersonDuplicateController extends Controller 'You are not allowed to see this person.' ); - $duplicatePersons = $this->personACLAwareRepository->findMatchingPersons( $person, 0.5, diff --git a/src/Bundle/ChillPersonBundle/Repository/PersonACLAwareRepository.php b/src/Bundle/ChillPersonBundle/Repository/PersonACLAwareRepository.php index 382ce59b7..5773d38c2 100644 --- a/src/Bundle/ChillPersonBundle/Repository/PersonACLAwareRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/PersonACLAwareRepository.php @@ -14,13 +14,11 @@ namespace Chill\PersonBundle\Repository; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Repository\CountryRepository; use Chill\MainBundle\Search\ParsingException; -use Chill\MainBundle\Search\SearchApi; use Chill\MainBundle\Search\SearchApiQuery; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Security\Authorization\PersonVoter; use Chill\PersonBundle\Templating\Entity\PersonRender; -use Chill\PersonBundle\Repository\PersonNotDuplicateRepository; use DateTimeInterface; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\NonUniqueResultException; @@ -43,11 +41,11 @@ final class PersonACLAwareRepository implements PersonACLAwareRepositoryInterfac private EntityManagerInterface $em; - private Security $security; + private PersonNotDuplicateRepository $personNotDuplicateRepository; private PersonRender $personRender; - private PersonNotDuplicateRepository $personNotDuplicateRepository; + private Security $security; public function __construct( Security $security, @@ -308,6 +306,70 @@ final class PersonACLAwareRepository implements PersonACLAwareRepositoryInterfac return $this->fetchQueryPerson($query); } + /** + * @throws NonUniqueResultException + * @throws ParsingException + * + * @return array|Person[] + */ + public function findMatchingPersons( + Person $person, + float $precision = 0.15, + string $orderBy = self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY + ): array { + $query = $this->matchPerson($person, $precision, $orderBy); + $authorizedQuery = $this->addAuthorizations($query); + + return $this->fetchQueryPerson($authorizedQuery); + } + + public function matchPerson( + Person $person, + float $precision = 0.15, + string $orderBy = self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY + ): SearchApiQuery { + $fullName = $this->personRender->renderString($person, []); + + $query = new SearchApiQuery(); + $query->setFromClause('chill_person_person AS person'); + $query->andWhereClause( + 'SIMILARITY(person.fullnameCanonical, UNACCENT(LOWER(?))) >= ?', + [$fullName, $precision] + ); + + if (null !== $person->getId()) { + $query->andWhereClause( + 'person.id != ?', + [$person->getId()] + ); + + $notDuplicatePersons = $this->personNotDuplicateRepository->findNotDuplicatePerson($person); + + if (count($notDuplicatePersons)) { + $query->andWhereClause( + 'person.id NOT IN (?)', + [$notDuplicatePersons] + ); + } + } + + switch ($orderBy) { + case self::SIMILAR_SEARCH_ORDER_BY_ALPHABETICAL: + $query->setSelectPertinence('person.fullnameCanonical'); + + break; + + case self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY: + default: + $query->setSelectPertinence( + 'SIMILARITY(person.fullnameCanonical, UNACCENT(LOWER(?)))', + [$fullName] + ); + } + + return $query; + } + private function addAuthorizations(SearchApiQuery $query): SearchApiQuery { $authorizedCenters = $this->authorizationHelper @@ -333,70 +395,4 @@ final class PersonACLAwareRepository implements PersonACLAwareRepositoryInterfac }, $authorizedCenters) ); } - - - /** - * @throws NonUniqueResultException - * @throws ParsingException - * - * @return array|Person[] - */ - public function findMatchingPersons( - Person $person, - float $precision = 0.15, - string $orderBy = self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY - ): array { - $query = $this->matchPerson($person, $precision, $orderBy); - $authorizedQuery = $this->addAuthorizations($query); - - return $this->fetchQueryPerson($authorizedQuery); - } - - public function matchPerson( - Person $person, - float $precision = 0.15, - string $orderBy = self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY - ): SearchApiQuery { - - $fullName = $this->personRender->renderString($person, []); - - $query = new SearchApiQuery(); - $query->setFromClause('chill_person_person AS person'); - $query->andWhereClause( - "SIMILARITY(person.fullnameCanonical, UNACCENT(LOWER(?))) >= ?", - [$fullName, $precision] - ); - - if (null !== $person->getId()) { - $query->andWhereClause( - "person.id != ?", - [$person->getId()] - ); - - $notDuplicatePersons = $this->personNotDuplicateRepository->findNotDuplicatePerson($person); - - if (count($notDuplicatePersons)) { - $query->andWhereClause( - "person.id NOT IN (?)", - [$notDuplicatePersons] - ); - } - } - - switch ($orderBy) { - case self::SIMILAR_SEARCH_ORDER_BY_ALPHABETICAL: - $query->setSelectPertinence('person.fullnameCanonical'); - break; - - case self::SIMILAR_SEARCH_ORDER_BY_SIMILARITY: - default: - $query->setSelectPertinence( - 'SIMILARITY(person.fullnameCanonical, UNACCENT(LOWER(?)))', - [$fullName] - ); - } - - return $query; - } - } diff --git a/src/Bundle/ChillPersonBundle/Search/SimilarPersonMatcher.php b/src/Bundle/ChillPersonBundle/Search/SimilarPersonMatcher.php index f919c38a2..967b5cc08 100644 --- a/src/Bundle/ChillPersonBundle/Search/SimilarPersonMatcher.php +++ b/src/Bundle/ChillPersonBundle/Search/SimilarPersonMatcher.php @@ -21,9 +21,9 @@ use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInt use function count; -/* -* @deprecated -*/ +/** + * @deprecated + */ class SimilarPersonMatcher { public const SIMILAR_SEARCH_ORDER_BY_ALPHABETICAL = 'alphabetical';