index/src/Cache/Valkey/ValkeyProviderInfo.php
2024-04-10 22:23:34 +00:00

136 lines
2.9 KiB
PHP

<?php
// ValkeyProviderInfo.php
// Created: 2024-04-10
// Updated: 2024-04-10
namespace Index\Cache\Valkey;
use InvalidArgumentException;
use RuntimeException;
use Index\Cache\ICacheProviderInfo;
use Index\Net\{DnsEndPoint,EndPoint,IPEndPoint,UnixEndPoint};
/**
* Represents Valkey provider info.
*/
class ValkeyProviderInfo implements ICacheProviderInfo {
public function __construct(
private EndPoint $endPoint,
private string $prefix,
private bool $persist,
private string $username,
private string $password,
private int $dbNumber,
) {}
/**
* Gets server endpoint.
*
* @return EndPoint
*/
public function getEndPoint(): EndPoint {
return $this->endPoint;
}
/**
* Retrieves the server hostname.
*
* @throws RuntimeException Unsupported endpoint specified.
* @return string
*/
public function getServerHost(): string {
if($this->endPoint instanceof UnixEndPoint)
return $this->endPoint->getPath();
if($this->endPoint instanceof DnsEndPoint)
return $this->endPoint->getHost();
if($this->endPoint instanceof IPEndPoint)
return $this->endPoint->getAddress()->getCleanAddress();
throw new RuntimeException('EndPoint type is not supported.');
}
/**
* Retrieves the server Unix path.
*
* @return int
*/
public function getServerPort(): int {
if($this->endPoint instanceof DnsEndPoint || $this->endPoint instanceof IPEndPoint)
return $this->endPoint->getPort();
return 0;
}
/**
* A prefix that gets applied to every key.
*
* @return string
*/
public function getPrefix(): string {
return $this->prefix;
}
/**
* Whether the connection should be persistent.
*
* @return bool
*/
public function isPersistent(): bool {
return $this->persist;
}
/**
* Whether a username should be used.
*
* @return bool
*/
public function hasUsername(): bool {
return $this->username !== '';
}
/**
* Username to authenticate with.
*
* @return string
*/
public function getUsername(): string {
return $this->username;
}
/**
* Whether a password should be used.
*
* @return bool
*/
public function hasPassword(): bool {
return $this->password !== '';
}
/**
* Password to authenticate with.
*
* @return string
*/
public function getPassword(): string {
return $this->password;
}
/**
* Whether a database should be selected.
*
* @return bool
*/
public function hasDatabaseNumber(): bool {
return $this->dbNumber !== 0;
}
/**
* Database that should be selected.
*
* @return int
*/
public function getDatabaseNumber(): int {
return $this->dbNumber;
}
}