diff --git a/src/Socialbox/Classes/ServerResolver.php b/src/Socialbox/Classes/ServerResolver.php index c74a656..d8338ca 100644 --- a/src/Socialbox/Classes/ServerResolver.php +++ b/src/Socialbox/Classes/ServerResolver.php @@ -3,12 +3,16 @@ namespace Socialbox\Classes; use InvalidArgumentException; + use Socialbox\Exceptions\DatabaseOperationException; use Socialbox\Exceptions\ResolutionException; use Socialbox\Managers\ResolvedDnsRecordsManager; use Socialbox\Objects\DnsRecord; class ServerResolver { + private static bool $mockingEnabled = false; + private static array $mockedRecords = []; + /** * Resolves a domain by retrieving and parsing its DNS TXT records. * Optionally checks a database for cached resolution data before performing a DNS query. @@ -17,9 +21,19 @@ * @param bool $useDatabase Whether to check the database for cached resolution data; defaults to true. * @return DnsRecord The parsed DNS record for the given domain. * @throws ResolutionException If the DNS TXT records cannot be retrieved or parsed. + * @throws DatabaseOperationException If an error occurs while interacting with the database. (Only if $useDatabase is true) */ public static function resolveDomain(string $domain, bool $useDatabase=true): DnsRecord { + // Return the mocked record if mocking is enabled + if(self::$mockingEnabled) + { + if(isset(self::$mockedRecords[$domain])) + { + return self::$mockedRecords[$domain]; + } + } + // Check the database if enabled if ($useDatabase) { @@ -91,4 +105,57 @@ } return $fullRecordBuilder; } + + /** + * Determines if mocking is enabled. + * + * @return bool True if mocking is enabled, false otherwise. + */ + public static function isMockingEnabled(): bool + { + return self::$mockingEnabled; + } + + /** + * Enables or disables mocking functionality. + * + * @param bool $enabled Indicates whether mocking should be enabled (true) or disabled (false). + * @return void + */ + public static function setMockingEnabled(bool $enabled): void + { + self::$mockingEnabled = $enabled; + } + + /** + * Retrieves the mocked records. + * + * @return array The list of mocked records. + */ + public static function getMockedRecords(): array + { + return self::$mockedRecords; + } + + /** + * Adds a mock DNS record for a specific domain. + * + * @param string $domain The domain name for which the DNS record is being mocked. + * @param DnsRecord $record The DNS record to be associated with the specified domain. + * @return void + */ + public static function mockRecord(string $domain, DnsRecord $record): void + { + self::$mockedRecords[$domain] = $record; + } + + /** + * Clears all mocked records by resetting the mocked records array. + * + * @return void + */ + public static function clearMockedRecords(): void + { + self::$mockedRecords = []; + } } \ No newline at end of file