Add mocking functionality to ServerResolver class
This commit is contained in:
parent
deb4667975
commit
bfe8064a94
1 changed files with 67 additions and 0 deletions
|
@ -3,12 +3,16 @@
|
||||||
namespace Socialbox\Classes;
|
namespace Socialbox\Classes;
|
||||||
|
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
|
use Socialbox\Exceptions\DatabaseOperationException;
|
||||||
use Socialbox\Exceptions\ResolutionException;
|
use Socialbox\Exceptions\ResolutionException;
|
||||||
use Socialbox\Managers\ResolvedDnsRecordsManager;
|
use Socialbox\Managers\ResolvedDnsRecordsManager;
|
||||||
use Socialbox\Objects\DnsRecord;
|
use Socialbox\Objects\DnsRecord;
|
||||||
|
|
||||||
class ServerResolver
|
class ServerResolver
|
||||||
{
|
{
|
||||||
|
private static bool $mockingEnabled = false;
|
||||||
|
private static array $mockedRecords = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolves a domain by retrieving and parsing its DNS TXT records.
|
* Resolves a domain by retrieving and parsing its DNS TXT records.
|
||||||
* Optionally checks a database for cached resolution data before performing a DNS query.
|
* 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.
|
* @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.
|
* @return DnsRecord The parsed DNS record for the given domain.
|
||||||
* @throws ResolutionException If the DNS TXT records cannot be retrieved or parsed.
|
* @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
|
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
|
// Check the database if enabled
|
||||||
if ($useDatabase)
|
if ($useDatabase)
|
||||||
{
|
{
|
||||||
|
@ -91,4 +105,57 @@
|
||||||
}
|
}
|
||||||
return $fullRecordBuilder;
|
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 = [];
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue