private function importEmail()

in app/Services/VCard/ImportVCard.php [790:839]


    private function importEmail(Contact $contact, VCard $entry): void
    {
        if (is_null($entry->EMAIL)) {
            return;
        }

        $contactFieldTypeId = $this->getContactFieldTypeId(ContactFieldType::EMAIL);
        if (! $contactFieldTypeId) {
            // Case of contact field type email does not exist
            return;
        }

        $emails = $contact->contactFields()
                            ->email()
                            ->get()
                            ->sortBy('id');

        foreach ($entry->EMAIL as $email) {
            $contactFieldContent = [
                'account_id' => $contact->account_id,
                'contact_id' => $contact->id,
                'contact_field_type_id' => $contactFieldTypeId,
                'data' => $this->formatValue((string) $email),
                'labels' => preg_split('/,/', (string) $email['TYPE']),
            ];

            // We assume contact fields are in the same order
            $contactField = $emails->shift();

            if (is_null($contactField)) {
                // Address does not exist
                app(CreateContactField::class)->execute($contactFieldContent);
            } else {
                // Address has to be updated
                app(UpdateContactField::class)->execute([
                    'contact_field_id' => $contactField->id,
                ] +
                    $contactFieldContent
                );
            }
        }

        foreach ($emails as $email) {
            // Remaining emails have to be removed
            app(DestroyContactField::class)->execute([
                'account_id' => $contact->account_id,
                'contact_field_id' => $email->id,
            ]);
        }
    }