Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Server/Socket/SocketFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace CrazyGoat\Forklift\Server\Socket;

use CrazyGoat\Forklift\Server\Types\ProtocolType;
use CrazyGoat\Forklift\Server\Types\ProxyType;

class SocketFactory
{
public static function create(ProxyType $type, int $port, ProtocolType $protocol): SocketProxyInterface
{
return match ($type) {
ProxyType::REUSE_PORT => self::createReuseOrFallback(),
ProxyType::FORK_SHARED => new ForkSharedProxy(),
ProxyType::MASTER => new MasterProxy(),
};
}

private static function createReuseOrFallback(): SocketProxyInterface
{
$proxy = new ReusePortProxy();

if ($proxy->isSupported()) {
return $proxy;
}

return new ForkSharedProxy();
}
}
52 changes: 52 additions & 0 deletions tests/Server/Socket/SocketFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace CrazyGoat\Forklift\Tests\Server\Socket;

use CrazyGoat\Forklift\Server\Socket\ForkSharedProxy;
use CrazyGoat\Forklift\Server\Socket\MasterProxy;
use CrazyGoat\Forklift\Server\Socket\ReusePortProxy;
use CrazyGoat\Forklift\Server\Socket\SocketFactory;
use CrazyGoat\Forklift\Server\Types\ProtocolType;
use CrazyGoat\Forklift\Server\Types\ProxyType;
use PHPUnit\Framework\TestCase;

class SocketFactoryTest extends TestCase
{
public function testCreateMasterReturnsMasterProxy(): void
{
$proxy = SocketFactory::create(ProxyType::MASTER, 8080, ProtocolType::TCP);

$this->assertInstanceOf(MasterProxy::class, $proxy);
}

public function testCreateForkSharedReturnsForkSharedProxy(): void
{
$proxy = SocketFactory::create(ProxyType::FORK_SHARED, 8080, ProtocolType::HTTP);

$this->assertInstanceOf(ForkSharedProxy::class, $proxy);
}

public function testCreateReusePortReturnsReusePortWhenSupported(): void
{
if (!(new ReusePortProxy())->isSupported()) {
$this->markTestSkipped('SO_REUSEPORT not supported on this platform');
}

$proxy = SocketFactory::create(ProxyType::REUSE_PORT, 8080, ProtocolType::HTTP);

$this->assertInstanceOf(ReusePortProxy::class, $proxy);
}

public function testCreateReusePortFallsBackToForkSharedWhenNotSupported(): void
{
if ((new ReusePortProxy())->isSupported()) {
$this->markTestSkipped('SO_REUSEPORT is supported on this platform');
}

$proxy = SocketFactory::create(ProxyType::REUSE_PORT, 8080, ProtocolType::HTTP);

$this->assertInstanceOf(ForkSharedProxy::class, $proxy);
}
}
Loading