Skip to content

Commit

Permalink
Replace Connection classes with single ConnectionConfig class
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Aug 28, 2024
1 parent a00adcf commit bdd3747
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 124 deletions.
26 changes: 11 additions & 15 deletions src/Bootloader/TemporalBridgeBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
use Spiral\Core\FactoryInterface;
use Spiral\RoadRunnerBridge\Bootloader\RoadRunnerBootloader;
use Spiral\TemporalBridge\Commands;
use Spiral\TemporalBridge\Config\ConnectionConfig;
use Spiral\TemporalBridge\Config\TemporalConfig;
use Spiral\TemporalBridge\Connection\Connection;
use Spiral\TemporalBridge\Connection\SslConnection;
use Spiral\TemporalBridge\DeclarationLocator;
use Spiral\TemporalBridge\DeclarationLocatorInterface;
use Spiral\TemporalBridge\DeclarationRegistryInterface;
Expand Down Expand Up @@ -156,11 +155,11 @@ protected function initConfig(EnvironmentInterface $env): void
$this->config->setDefaults(
TemporalConfig::CONFIG,
[
// 'address' => $env->get('TEMPORAL_ADDRESS', '127.0.0.1:7233'),
// 'namespace' => 'App\\Endpoint\\Temporal\\Workflow',
'connection' => $env->get('TEMPORAL_CONNECTION', 'default'),
'connections' => [
'default' => new Connection(address: $env->get('TEMPORAL_ADDRESS', '127.0.0.1:7233')),
'default' => ConnectionConfig::createInsecure(
address: $env->get('TEMPORAL_ADDRESS', '127.0.0.1:7233'),
),
],
'defaultWorker' => (string)$env->get(
'TEMPORAL_TASK_QUEUE',
Expand All @@ -176,17 +175,14 @@ protected function initServiceClient(TemporalConfig $config): ServiceClientInter
{
$connection = $config->getConnection($config->getDefaultConnection());

if ($connection instanceof SslConnection) {
return ServiceClient::createSSL(
return $connection->secure
? ServiceClient::createSSL(
address: $connection->address,
crt: $connection->crt,
clientKey: $connection->clientKey,
clientPem: $connection->clientPem,
overrideServerName: $connection->overrideServerName,
);
}

return ServiceClient::create(address: $connection->address);
crt: $connection->rootCerts,
clientKey: $connection->privateKey,
clientPem: $connection->certChain,
)
: ServiceClient::create(address: $connection->address);
}

protected function initPipelineProvider(TemporalConfig $config, FactoryInterface $factory): PipelineProvider
Expand Down
77 changes: 77 additions & 0 deletions src/Config/ConnectionConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Spiral\TemporalBridge\Config;

/**
* Temporal connection configuration.
*
* How to connect to local Temporal server:
*
* ```php
* ConnectionConfig::createInsecure('localhost:7233')
* ```
*
* How to connect to Temporal Cloud:
*
* ```php
* ConnectionConfig::createCloud(
* address: 'foo-bar-default.baz.tmprl.cloud:7233',
* privateKey: '/my-project.key',
* certChain: '/my-project.pem',
* )
* ```
*/
final class ConnectionConfig
{
private function __construct(
public readonly string $address,
public readonly bool $secure = false,
public readonly ?string $rootCerts = null,
public readonly ?string $privateKey = null,
public readonly ?string $certChain = null,
) {}

/**
* @param non-empty-string $address
*/
public static function createInsecure(
string $address,
): self {
return new self($address);
}

/**
* @param non-empty-string $address
* @param non-empty-string|null $rootCerts Root certificates string or file in PEM format.
* If null provided, default gRPC root certificates are used.
* @param non-empty-string|null $privateKey Client private key string or file in PEM format.
* @param non-empty-string|null $certChain Client certificate chain string or file in PEM format.
*/
public static function createSecure(
string $address,
?string $rootCerts = null,
?string $privateKey = null,
?string $certChain = null,
): self {
return new self($address, true, $rootCerts, $privateKey, $certChain);
}

/**
* Used to connect to Temporal Cloud.
*
* @link https://docs.temporal.io/cloud/get-started
*
* @param non-empty-string $address
* @param non-empty-string $privateKey Client private key string or file in PEM format.
* @param non-empty-string $certChain Client certificate chain string or file in PEM format.
*/
public static function createCloud(
string $address,
string $privateKey,
string $certChain,
): self {
return new self($address, true, null, $privateKey, $certChain);
}
}
9 changes: 3 additions & 6 deletions src/Config/TemporalConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

use Spiral\Core\Container\Autowire;
use Spiral\Core\InjectableConfig;
use Spiral\TemporalBridge\Connection\Connection;
use Spiral\TemporalBridge\Connection\DsnConnection;
use Spiral\TemporalBridge\Connection\SslConnection;
use Temporal\Client\ClientOptions;
use Temporal\Exception\ExceptionInterceptorInterface;
use Temporal\Internal\Interceptor\Interceptor;
Expand All @@ -26,7 +23,7 @@
* @property array{
* address?: non-empty-string|null,
* connection: non-empty-string,
* connections: array<non-empty-string, Connection>,
* connections: array<non-empty-string, ConnectionConfig>,
* temporalNamespace: non-empty-string,
* defaultWorker: non-empty-string,
* workers: array<non-empty-string, WorkerOptions|TWorker>,
Expand Down Expand Up @@ -62,7 +59,7 @@ public function getDefaultConnection(): string
return $this->config['connection'] ?? 'default';
}

public function getConnection(string $name): Connection
public function getConnection(string $name): ConnectionConfig
{
// Legacy support. Will be removed in further versions.
// If you read this, please remove address from your configuration and use connections instead.
Expand All @@ -72,7 +69,7 @@ public function getConnection(string $name): Connection
'Using `address` is deprecated, use `connections` instead.',
\E_USER_DEPRECATED,
);
return new Connection(address: $address);
return ConnectionConfig::createInsecure(address: $address);
}

if (isset($this->config['connections'][$name])) {
Expand Down
20 changes: 0 additions & 20 deletions src/Connection/Connection.php

This file was deleted.

25 changes: 0 additions & 25 deletions src/Connection/SslConnection.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/Connection/TemporalCloudConnection.php

This file was deleted.

21 changes: 9 additions & 12 deletions tests/app/config/temporal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@

declare(strict_types=1);

use Spiral\TemporalBridge\Connection\Connection;
use Spiral\TemporalBridge\Connection\SslConnection;
use Spiral\TemporalBridge\Connection\TemporalCloudConnection;
use Spiral\TemporalBridge\Config\ConnectionConfig;

return [
'connection' => env('TEMPORAL_CONNECTION', 'default'),
'connections' => [
'default' => new Connection(
'default' => ConnectionConfig::createInsecure(
address: 'localhost:7233',
),
'ssl' => new SslConnection(
'ssl' => ConnectionConfig::createSecure(
address: 'ssl:7233',
crt: '/path/to/crt',
clientKey: '/path/to/clientKey',
clientPem: '/path/to/clientPem',
overrideServerName: 'overrideServerName',
rootCerts: '/path/to/crt',
privateKey: '/path/to/clientKey',
certChain: '/path/to/clientPem',
),
'temporal_cloud' => new TemporalCloudConnection(
'temporal_cloud' => ConnectionConfig::createCloud(
address: 'ssl:7233',
clientKey: '/path/to/clientKey',
clientPem: '/path/to/clientPem',
privateKey: '/path/to/clientKey',
certChain: '/path/to/clientPem',
),
],
];
2 changes: 1 addition & 1 deletion tests/src/Bootloader/TemporalBridgeBootloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function testConnection(): void
}

#[Env('TEMPORAL_CONNECTION', 'ssl')]
public function testSslConnection(): void
public function testSecureConnection(): void
{
$client = $this->getContainer()->get(ServiceClientInterface::class);

Expand Down
25 changes: 10 additions & 15 deletions tests/src/Config/TemporalConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace Spiral\TemporalBridge\Tests\Config;

use Spiral\TemporalBridge\Config\ConnectionConfig;
use Spiral\TemporalBridge\Config\TemporalConfig;
use Spiral\TemporalBridge\Connection\Connection;
use Spiral\TemporalBridge\Connection\DsnConnection;
use Spiral\TemporalBridge\Connection\SslConnection;
use Spiral\TemporalBridge\Tests\TestCase;
use Temporal\Client\ClientOptions;
use Temporal\Worker\WorkerFactoryInterface;
Expand Down Expand Up @@ -38,7 +36,7 @@ public function testGetConnectionFromAddress(): void
]);

$connection = $config->getConnection('default');
$this->assertSame(Connection::class, $connection::class);
$this->assertSame(ConnectionConfig::class, $connection::class);

$this->assertSame('localhost:1111', $connection->address);
}
Expand All @@ -47,25 +45,22 @@ public function testGetSslConnection(): void
{
$config = new TemporalConfig([
'connections' => [
'default' => new SslConnection(
'default' => ConnectionConfig::createSecure(
address: 'localhost:2222',
crt: 'crt',
clientKey: 'clientKey',
clientPem: 'clientPem',
overrideServerName: 'overrideServerName',
rootCerts: 'crt',
privateKey: 'clientKey',
certChain: 'clientPem',
),
],
]);

$connection = $config->getConnection('default');

$this->assertSame(SslConnection::class, $connection::class);

$this->assertTrue($connection->secure);
$this->assertSame('localhost:2222', $connection->address);
$this->assertSame('crt', $connection->crt);
$this->assertSame('clientKey', $connection->clientKey);
$this->assertSame('clientPem', $connection->clientPem);
$this->assertSame('overrideServerName', $connection->overrideServerName);
$this->assertSame('crt', $connection->rootCerts);
$this->assertSame('clientKey', $connection->privateKey);
$this->assertSame('clientPem', $connection->certChain);
}

public function testGetsDefaultWorker(): void
Expand Down

0 comments on commit bdd3747

Please sign in to comment.