public function processAddresses($contact, $addresses)
{
$getAddressId = function ($addressRelation) {
return $addressRelation->getAddress()->getId();
};
$delete = function ($addressRelation) use($contact) {
$this->removeAddressRelation($contact, $addressRelation);
return true;
};
$update = function ($addressRelation, $matchedEntry) use($contact) {
$address = $addressRelation->getAddress();
$result = $this->updateAddress($address, $matchedEntry, $isMain);
if ($isMain) {
$this->unsetMain($this->getAddressRelations($contact));
}
$addressRelation->setMain($isMain);
return $result;
};
$add = function ($addressData) use($contact) {
$address = $this->createAddress($addressData, $isMain);
$this->addAddress($contact, $address, $isMain);
return true;
};
$entities = $this->getAddressRelations($contact);
$result = $this->processSubEntities($entities, $addresses, $getAddressId, $add, $update, $delete);
$this->resetIndexOfSubentites($entities);
// check if main exists, else take first address
$this->checkAndSetMainAddress($this->getAddressRelations($contact));
return $result;
}