1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
<?php
declare(strict_types=1);
namespace Pheanstalk;
use Pheanstalk\Contract\SocketFactoryInterface;
use Pheanstalk\Contract\SocketInterface;
use Pheanstalk\Exception\NoImplementationException;
use Pheanstalk\Socket\FsockopenSocket;
use Pheanstalk\Socket\SocketSocket;
use Pheanstalk\Socket\StreamSocket;
use Pheanstalk\Values\SocketImplementation;
use Pheanstalk\Values\Timeout;
final class SocketFactory implements SocketFactoryInterface
{
private const DEFAULT_TIMEOUT = 10;
public readonly SocketImplementation $implementation;
private readonly Timeout $connectTimeout;
private readonly Timeout $receiveTimeout;
private readonly Timeout $sendTimeout;
/**
* @param string $host
* @param int $port
* @param SocketImplementation|null $implementation
* @param Timeout|null $connectTimeout
* @param Timeout|null $receiveTimeout
* @param Timeout|null $sendTimeout the timeout used for sending data, not supported by all implementations
* @throws NoImplementationException
*/
public function __construct(
private readonly string $host,
private readonly int $port = self::DEFAULT_PORT,
null|SocketImplementation $implementation = null,
?Timeout $connectTimeout = null,
?Timeout $receiveTimeout = null,
?Timeout $sendTimeout = null,
) {
$this->implementation = $implementation ?? $this->detectImplementation();
$this->connectTimeout = $connectTimeout ?? new Timeout(self::DEFAULT_TIMEOUT, 0);
$this->receiveTimeout = $receiveTimeout ?? new Timeout(self::DEFAULT_TIMEOUT, 0);
$this->sendTimeout = $sendTimeout ?? new Timeout(self::DEFAULT_TIMEOUT, 0);
}
private function detectImplementation(): SocketImplementation
{
// Prefer socket
if (extension_loaded('sockets')) {
return SocketImplementation::SOCKET;
}
// Then fall back to stream
if (function_exists('stream_socket_client')) {
return SocketImplementation::STREAM;
}
// Then fall back to fsockopen
if (function_exists('fsockopen')) {
return SocketImplementation::FSOCKOPEN;
}
throw new NoImplementationException();
}
private function createStreamSocket(): StreamSocket
{
return new StreamSocket($this->host, $this->port, $this->connectTimeout, $this->receiveTimeout);
}
private function createSocketSocket(): SocketSocket
{
return new SocketSocket($this->host, $this->port, $this->connectTimeout, $this->sendTimeout, $this->receiveTimeout);
}
private function createFsockopenSocket(): FsockopenSocket
{
return new FsockopenSocket($this->host, $this->port, $this->connectTimeout, $this->receiveTimeout);
}
/**
* This function must return a connected socket that is ready for reading / writing.
* @return SocketInterface
*/
public function create(): SocketInterface
{
return match ($this->implementation) {
SocketImplementation::SOCKET => $this->createSocketSocket(),
SocketImplementation::STREAM => $this->createStreamSocket(),
SocketImplementation::FSOCKOPEN => $this->createFsockopenSocket()
};
}
}
|