Skip to content

Commit

Permalink
pass context options when creating client from data source name (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch authored Oct 13, 2020
1 parent 107a88a commit 3d76923
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
34 changes: 29 additions & 5 deletions src/Protocol/Imap/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ public function newClient(): Client

/**
* @param string $dataSourceName
* @param array<string, array<string, mixed>> $contextOptions
* @return ClientFactory
*/
public static function fromString(string $dataSourceName):ClientFactory
public static function fromString(string $dataSourceName, array $contextOptions = []):ClientFactory
{
$components = \parse_url($dataSourceName);
if ($components === false || !isset($components['scheme'], $components['host'])) {
Expand All @@ -190,27 +191,48 @@ public static function fromString(string $dataSourceName):ClientFactory
}

$insecureConnectionAllowed = false;
$startTls = true;

switch ($components['scheme']) {
case 'imap':
$connection = new PlainTcpConnection(
$components['host'],
$components['port'] ?? 143
$components['port'] ?? 143,
1.0,
$contextOptions
);
break;
case 'imaps':
$connection = new SecureConnection(
$components['host'],
$components['port'] ?? 993,
new SecureConnectionOptions(
(int)($query['crypto'] ?? CryptoConstant::getDefaultMethod(PHP_VERSION))
(int)($query['crypto'] ?? CryptoConstant::getDefaultMethod(PHP_VERSION)),
10,
$contextOptions
)
);
break;
case 'imap-starttls':
$insecureConnectionAllowed = true;
$startTls = false;

$connection = new PlainTcpConnection(
$components['host'],
$components['port'] ?? 143,
1.0,
$contextOptions
);
break;
case 'imap+starttls':
$insecureConnectionAllowed = true;
$startTls = true;

$connection = new PlainTcpConnection(
$components['host'],
$components['port'] ?? 143
$components['port'] ?? 143,
1.0,
$contextOptions
);
break;
default:
Expand All @@ -237,7 +259,9 @@ public static function fromString(string $dataSourceName):ClientFactory
$factory->reconnectAfter = $query['reconnectAfter'];
}

if (isset($query['crypto'])) {
if (!$startTls) {
$factory->startTls = 0;
} elseif (isset($query['crypto'])) {
$factory->startTls = (int)$query['crypto'];
}

Expand Down
19 changes: 14 additions & 5 deletions src/Protocol/Smtp/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ public function newClient(): Client

/**
* @param string $dataSourceName
* @param array<string, array<string, mixed>> $contextOptions
* @return ClientFactory
*/
public static function fromString(string $dataSourceName):ClientFactory
public static function fromString(string $dataSourceName, array $contextOptions = []):ClientFactory
{
$components = \parse_url($dataSourceName);
if ($components === false || !isset($components['scheme']) || !isset($components['host'])) {
Expand All @@ -210,15 +211,19 @@ public static function fromString(string $dataSourceName):ClientFactory
case 'smtp':
$connection = new PlainTcpConnection(
$components['host'],
$components['port'] ?? 587
$components['port'] ?? 587,
1.0,
$contextOptions
);
break;
case 'smtps':
$connection = new SecureConnection(
$components['host'],
$components['port'] ?? 465,
new SecureConnectionOptions(
(int)($query['crypto'] ?? CryptoConstant::getDefaultMethod(PHP_VERSION))
(int)($query['crypto'] ?? CryptoConstant::getDefaultMethod(PHP_VERSION)),
10,
$contextOptions
)
);
break;
Expand All @@ -228,7 +233,9 @@ public static function fromString(string $dataSourceName):ClientFactory

$connection = new PlainTcpConnection(
$components['host'],
$components['port'] ?? 25
$components['port'] ?? 25,
1.0,
$contextOptions
);
break;
case 'smtp+starttls':
Expand All @@ -237,7 +244,9 @@ public static function fromString(string $dataSourceName):ClientFactory

$connection = new PlainTcpConnection(
$components['host'],
$components['port'] ?? 25
$components['port'] ?? 25,
1.0,
$contextOptions
);
break;
default:
Expand Down

0 comments on commit 3d76923

Please sign in to comment.