Files
michaelschiemer/src/Infrastructure/GeoIp/GeoIp.php
Michael Schiemer 5050c7d73a docs: consolidate documentation into organized structure
- Move 12 markdown files from root to docs/ subdirectories
- Organize documentation by category:
  • docs/troubleshooting/ (1 file)  - Technical troubleshooting guides
  • docs/deployment/      (4 files) - Deployment and security documentation
  • docs/guides/          (3 files) - Feature-specific guides
  • docs/planning/        (4 files) - Planning and improvement proposals

Root directory cleanup:
- Reduced from 16 to 4 markdown files in root
- Only essential project files remain:
  • CLAUDE.md (AI instructions)
  • README.md (Main project readme)
  • CLEANUP_PLAN.md (Current cleanup plan)
  • SRC_STRUCTURE_IMPROVEMENTS.md (Structure improvements)

This improves:
 Documentation discoverability
 Logical organization by purpose
 Clean root directory
 Better maintainability
2025-10-05 11:05:04 +02:00

136 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Infrastructure\GeoIp;
use App\Framework\Http\IpAddress;
use App\Framework\Database\PdoConnection;
use App\Framework\Core\ValueObjects\CountryCode;
use PDO;
final class GeoIp
{
private readonly PDO $database;
private readonly IpRangeService $ipRangeService;
private readonly CountryDataService $countryDataService;
public function __construct(?string $databasePath = null)
{
$databasePath = $databasePath ?? __DIR__ . '/data/ip_country.sqlite';
$this->database = $this->initializeDatabase($databasePath);
$connection = new PdoConnection($this->database);
$this->ipRangeService = new IpRangeService($connection);
$this->countryDataService = new CountryDataService($connection);
}
/**
* @deprecated Use DatabaseSetup::setupCompleteDatabase() instead
*/
public function loadDatabase(): void
{
$setup = new DatabaseSetup(__DIR__ . '/data/ip_country.sqlite');
$setup->setupIpRangesOnly();
}
public function getCountryForString(string $ip): ?string
{
return $this->ipRangeService->getCountryCodeForIp($ip);
}
public function getCountryForIp(IpAddress $ipAddress): ?string
{
if ($ipAddress->isPrivate()) {
return null;
}
return $this->getCountryForString($ipAddress->value);
}
public function getCountryCode(IpAddress $ipAddress): CountryCode
{
if ($ipAddress->isPrivate()) {
// Return a default country code for private IPs (e.g., local development)
return CountryCode::fromString('XX'); // XX = Unknown/Private
}
$countryCodeString = $this->getCountryForString($ipAddress->value);
if ($countryCodeString === null) {
return CountryCode::fromString('XX'); // Unknown
}
return CountryCode::fromString($countryCodeString);
}
public function getCountryInfo(string $ip): CountryInfo
{
$countryCode = $this->getCountryForString($ip);
if (! $countryCode) {
return new CountryInfo($ip, null);
}
$country = $this->countryDataService->getCountryByCode($countryCode);
return new CountryInfo($ip, $countryCode, $country);
}
public function getCountryInfoForIp(IpAddress $ipAddress): CountryInfo
{
if ($ipAddress->isPrivate()) {
return new CountryInfo($ipAddress->value, null);
}
return $this->getCountryInfo($ipAddress->value);
}
public function getCountryNameGerman(string $ip): ?string
{
return $this->getCountryInfo($ip)->getGermanName();
}
public function getCountryNameEnglish(string $ip): ?string
{
return $this->getCountryInfo($ip)->getEnglishName();
}
public function getCountryNameNative(string $ip): ?string
{
return $this->getCountryInfo($ip)->getNativeName();
}
private function initializeDatabase(string $file): PDO
{
$directory = dirname($file);
if (! is_dir($directory)) {
mkdir($directory, 0755, true);
}
$db = new PDO('sqlite:' . $file);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// IP-Ranges Tabelle
$db->exec('CREATE TABLE IF NOT EXISTS ip_ranges (
id INTEGER PRIMARY KEY,
ip_start INTEGER,
ip_end INTEGER,
country TEXT
)');
$db->exec('CREATE INDEX IF NOT EXISTS idx_ip_range ON ip_ranges (ip_start, ip_end)');
// Länder-Tabelle für ISO-Daten
$db->exec('CREATE TABLE IF NOT EXISTS countries (
code TEXT PRIMARY KEY,
name_en TEXT,
name_de TEXT,
name_native TEXT,
updated_at TEXT
)');
return $db;
}
}