dbUpdater = $dbUpdater; $this->geoLiteDbReader = $geoLiteDbReader; } /** * @throws GeolocationDbUpdateFailedException */ public function checkDbUpdate(callable $mustBeUpdated = null, callable $handleProgress = null): void { try { $meta = $this->geoLiteDbReader->metadata(); if ($this->buildIsTooOld($meta->__get('buildEpoch'))) { $this->downloadNewDb(true, $mustBeUpdated, $handleProgress); } } catch (InvalidArgumentException $e) { // This is the exception thrown by the reader when the database file does not exist $this->downloadNewDb(false, $mustBeUpdated, $handleProgress); } } private function buildIsTooOld(int $buildTimestamp): bool { $buildDate = Chronos::createFromTimestamp($buildTimestamp); $now = Chronos::now(); return $now->gt($buildDate->addDays(35)); } /** * @throws GeolocationDbUpdateFailedException */ private function downloadNewDb( bool $olderDbExists, callable $mustBeUpdated = null, callable $handleProgress = null ): void { if ($mustBeUpdated !== null) { $mustBeUpdated($olderDbExists); } try { $this->dbUpdater->downloadFreshCopy($handleProgress); } catch (RuntimeException $e) { throw GeolocationDbUpdateFailedException::create($olderDbExists, $e); } } }