|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Services\RecordMatcher\Providers; |
| 4 | + |
| 5 | +use App\Models\Person; |
| 6 | +use Illuminate\Support\Facades\Http; |
| 7 | +use Illuminate\Support\Facades\Log; |
| 8 | +use Exception; |
| 9 | + |
| 10 | +/** |
| 11 | + * Ancestry provider for searching external genealogy records. |
| 12 | + * Integrates with Ancestry API to find potential matches. |
| 13 | + */ |
| 14 | +class AncestryProvider implements ExternalRecordProviderInterface |
| 15 | +{ |
| 16 | + protected string $apiKey; |
| 17 | + protected string $baseUrl; |
| 18 | + protected int $timeout; |
| 19 | + |
| 20 | + public function __construct() |
| 21 | + { |
| 22 | + $this->apiKey = config('services.ancestry.api_key', ''); |
| 23 | + $this->baseUrl = config('services.ancestry.base_url', 'https://api.ancestry.com/v1'); |
| 24 | + $this->timeout = config('services.ancestry.timeout', 30); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Search Ancestry for matching records. |
| 29 | + * |
| 30 | + * @param Person|int $localPerson |
| 31 | + * @return array |
| 32 | + */ |
| 33 | + public function search($localPerson): array |
| 34 | + { |
| 35 | + $person = is_int($localPerson) ? Person::find($localPerson) : $localPerson; |
| 36 | + |
| 37 | + if (!$person) { |
| 38 | + return []; |
| 39 | + } |
| 40 | + |
| 41 | + // If API key is not configured, return empty results |
| 42 | + if (empty($this->apiKey)) { |
| 43 | + Log::warning('Ancestry API key not configured'); |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + try { |
| 48 | + $searchParams = $this->buildSearchParams($person); |
| 49 | + $response = $this->performSearch($searchParams); |
| 50 | + |
| 51 | + return $this->parseResponse($response); |
| 52 | + } catch (Exception $e) { |
| 53 | + Log::error('Ancestry search failed', [ |
| 54 | + 'person_id' => $person->id, |
| 55 | + 'error' => $e->getMessage(), |
| 56 | + ]); |
| 57 | + return []; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Build search parameters from person data. |
| 63 | + * |
| 64 | + * @param Person $person |
| 65 | + * @return array |
| 66 | + */ |
| 67 | + protected function buildSearchParams(Person $person): array |
| 68 | + { |
| 69 | + $params = []; |
| 70 | + |
| 71 | + // Name parameters |
| 72 | + if ($person->first_name) { |
| 73 | + $params['givenName'] = $person->first_name; |
| 74 | + } |
| 75 | + if ($person->last_name) { |
| 76 | + $params['surname'] = $person->last_name; |
| 77 | + } |
| 78 | + |
| 79 | + // Birth information |
| 80 | + if ($person->birthday) { |
| 81 | + $params['birthYear'] = $person->birthday->format('Y'); |
| 82 | + } |
| 83 | + |
| 84 | + if ($person->birthplace) { |
| 85 | + $params['birthLocation'] = $person->birthplace->place ?? null; |
| 86 | + } |
| 87 | + |
| 88 | + // Death information |
| 89 | + if ($person->deathday) { |
| 90 | + $params['deathYear'] = $person->deathday->format('Y'); |
| 91 | + } |
| 92 | + |
| 93 | + if ($person->deathplace) { |
| 94 | + $params['deathLocation'] = $person->deathplace->place ?? null; |
| 95 | + } |
| 96 | + |
| 97 | + // Gender |
| 98 | + if ($person->sex) { |
| 99 | + $params['gender'] = $person->sex; |
| 100 | + } |
| 101 | + |
| 102 | + return array_filter($params); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Perform the actual API search. |
| 107 | + * |
| 108 | + * @param array $searchParams |
| 109 | + * @return array |
| 110 | + */ |
| 111 | + protected function performSearch(array $searchParams): array |
| 112 | + { |
| 113 | + $response = Http::timeout($this->timeout) |
| 114 | + ->withHeaders([ |
| 115 | + 'Authorization' => 'Bearer ' . $this->apiKey, |
| 116 | + 'Accept' => 'application/json', |
| 117 | + ]) |
| 118 | + ->get($this->baseUrl . '/search/records', $searchParams); |
| 119 | + |
| 120 | + if (!$response->successful()) { |
| 121 | + throw new Exception('Ancestry API request failed: ' . $response->status()); |
| 122 | + } |
| 123 | + |
| 124 | + return $response->json() ?? []; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Parse API response into standardized format. |
| 129 | + * |
| 130 | + * @param array $response |
| 131 | + * @return array |
| 132 | + */ |
| 133 | + protected function parseResponse(array $response): array |
| 134 | + { |
| 135 | + $results = []; |
| 136 | + $records = $response['records'] ?? $response['searchResults'] ?? []; |
| 137 | + |
| 138 | + foreach ($records as $record) { |
| 139 | + $person = $record['person'] ?? $record; |
| 140 | + |
| 141 | + $results[] = [ |
| 142 | + 'id' => $person['id'] ?? $person['personId'] ?? null, |
| 143 | + 'external_id' => $person['id'] ?? $person['personId'] ?? null, |
| 144 | + 'tree_id' => $person['treeId'] ?? null, |
| 145 | + 'first_name' => $person['givenName'] ?? $person['firstName'] ?? '', |
| 146 | + 'last_name' => $person['surname'] ?? $person['lastName'] ?? '', |
| 147 | + 'birth_year' => $person['birthYear'] ?? null, |
| 148 | + 'birth_date' => $person['birthDate'] ?? null, |
| 149 | + 'birth_place' => $person['birthLocation'] ?? $person['birthPlace'] ?? null, |
| 150 | + 'death_year' => $person['deathYear'] ?? null, |
| 151 | + 'death_date' => $person['deathDate'] ?? null, |
| 152 | + 'death_place' => $person['deathLocation'] ?? $person['deathPlace'] ?? null, |
| 153 | + 'gender' => $person['gender'] ?? $person['sex'] ?? null, |
| 154 | + 'parents' => $person['parents'] ?? null, |
| 155 | + 'spouse' => $person['spouse'] ?? null, |
| 156 | + 'children' => $person['children'] ?? [], |
| 157 | + 'source_url' => $person['recordUrl'] ?? $person['url'] ?? null, |
| 158 | + 'tree_name' => $person['treeName'] ?? null, |
| 159 | + 'tree_owner' => $person['treeOwner'] ?? null, |
| 160 | + ]; |
| 161 | + } |
| 162 | + |
| 163 | + return $results; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Get provider name. |
| 168 | + * |
| 169 | + * @return string |
| 170 | + */ |
| 171 | + public function getName(): string |
| 172 | + { |
| 173 | + return 'Ancestry'; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Check if provider is configured. |
| 178 | + * |
| 179 | + * @return bool |
| 180 | + */ |
| 181 | + public function isConfigured(): bool |
| 182 | + { |
| 183 | + return !empty($this->apiKey); |
| 184 | + } |
| 185 | +} |
0 commit comments