cs: Use Collection more and fix potential duplicates.

This commit is contained in:
Pol Dellaiera 2022-01-04 15:55:36 +01:00
parent bcd25fa950
commit 33913867ee
No known key found for this signature in database
GPG Key ID: D476DFE9C67467CA

View File

@ -22,6 +22,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use function in_array;
class PersonApiController extends ApiController
{
@ -79,6 +80,8 @@ class PersonApiController extends ApiController
{
$this->denyAccessUnlessGranted(PersonVoter::SEE, $person);
$seenAddressIds = [];
// collect addresses from location in courses
$addresses = $person
->getAccompanyingPeriodParticipations()
@ -91,13 +94,30 @@ class PersonApiController extends ApiController
static function (AccompanyingPeriodParticipation $accompanyingPeriodParticipation): ?Address {
return $accompanyingPeriodParticipation->getAccompanyingPeriod()->getAddressLocation();
}
)
->filter(
// We remove potential null addresses.
static fn (?Address $address): bool => null !== $address
)
->filter(
static function (Address $address) use (&$seenAddressIds): bool {
$id = $address->getId();
if (in_array($id, $seenAddressIds, true)) {
return false;
}
$seenAddressIds[] = $id;
return true;
}
);
// remove the actual address
$actual = $person->getCurrentHouseholdAddress();
if (null !== $actual) {
$addresses = $addresses->filter(static fn (?Address $address): bool => $address !== $actual);
$addresses = $addresses->filter(static fn (Address $address): bool => $address !== $actual);
}
return $this->json(