Skip to content

Commit 4643a6b

Browse files
committed
Convert exceptions to static methods
1 parent ba240da commit 4643a6b

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

lib/Doctrine/ODM/MongoDB/Configuration.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function getDriverOptions(): array
180180
public function getClientEncryptionOptions(): array
181181
{
182182
if (! isset($this->attributes['kmsProvider'])) {
183-
throw new ConfigurationException('MongoDB client encryption options are not set in configuration');
183+
throw ConfigurationException::clientEncryptionOptionsNotSet();
184184
}
185185

186186
return array_intersect_key($this->getAutoEncryptionOptions(), [
@@ -719,11 +719,11 @@ public function isLazyGhostObjectEnabled(): bool
719719
public function setKmsProvider(array $kmsProvider): void
720720
{
721721
if (! isset($kmsProvider['name'])) {
722-
throw new ConfigurationException('The "name" KMS provider option is required.');
722+
throw ConfigurationException::kmsProviderNameRequired();
723723
}
724724

725725
if (! is_string($kmsProvider['name'])) {
726-
throw new ConfigurationException('The "name" KMS provider option must be a non-empty string.');
726+
throw ConfigurationException::kmsProviderNameMustBeString();
727727
}
728728

729729
$this->attributes['kmsProvider'] = $kmsProvider;
@@ -749,7 +749,7 @@ public function setDefaultMasterKey(?array $masterKey): void
749749
public function setAutoEncryption(array $options): void
750750
{
751751
if (isset($options['kmsProviders'])) {
752-
throw new ConfigurationException('The "kmsProviders" encryption option must be set using the "setKmsProvider()" method.');
752+
throw ConfigurationException::kmsProvidersOptionMustUseSetter();
753753
}
754754

755755
$this->attributes['autoEncryption'] = $options;
@@ -774,7 +774,7 @@ public function getDefaultMasterKey(): ?array
774774
return null;
775775
}
776776

777-
return $this->attributes['defaultMasterKey'] ?? throw new ConfigurationException(sprintf('The "masterKey" configuration is required for the KMS provider "%s".', $this->attributes['kmsProvider']['name']));
777+
return $this->attributes['defaultMasterKey'] ?? throw ConfigurationException::masterKeyRequired($this->attributes['kmsProvider']['name']);
778778
}
779779

780780
private static function getVersion(): string

lib/Doctrine/ODM/MongoDB/ConfigurationException.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,28 @@ public static function proxyDirMissing(): self
2828
return new self('No proxy directory was configured. Please set a target directory first!');
2929
}
3030

31-
public static function kmsProvidersNotSupported(): self
31+
public static function clientEncryptionOptionsNotSet(): self
3232
{
33-
return new self('Setting multiple KMS providers is not supported. Please set a single KMS provider in your configuration.');
33+
return new self('MongoDB client encryption options are not set in configuration');
34+
}
35+
36+
public static function kmsProviderNameRequired(): self
37+
{
38+
return new self('The KMS provider "name" is required.');
39+
}
40+
41+
public static function kmsProviderNameMustBeString(): self
42+
{
43+
return new self('The KMS provider "name" must be a non-empty string.');
44+
}
45+
46+
public static function kmsProvidersOptionMustUseSetter(): self
47+
{
48+
return new self('The "kmsProviders" encryption option must be set using the "setKmsProvider()" method.');
49+
}
50+
51+
public static function masterKeyRequired(string $provider): self
52+
{
53+
return new self(sprintf('The "masterKey" configuration is required for the KMS provider "%s".', $provider));
3454
}
3555
}

tests/Doctrine/ODM/MongoDB/Tests/ConfigurationTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public function testMissingDefaultMasterKey(): void
118118
$c->setKmsProvider(['name' => 'aws', 'accessKeyId' => 'AKIA', 'secretAccessKey' => 'SECRET']);
119119

120120
self::expectException(ConfigurationException::class);
121+
self::expectExceptionMessage('The "masterKey" configuration is required for the KMS provider "aws".');
121122
$c->getDefaultMasterKey();
122123
}
123124

@@ -126,6 +127,35 @@ public function testKmsProvidersIsForbiddenInAutoEncryptionOptions(): void
126127
$c = new Configuration();
127128

128129
self::expectException(ConfigurationException::class);
130+
self::expectExceptionMessage('The "kmsProviders" encryption option must be set using the "setKmsProvider()" method.');
129131
$c->setAutoEncryption(['kmsProviders' => ['aws' => ['accessKeyId' => 'AKIA', 'secretAccessKey' => 'SECRET']]]);
130132
}
133+
134+
public function testClientEncryptionOptionsNotSet(): void
135+
{
136+
$c = new Configuration();
137+
self::expectException(ConfigurationException::class);
138+
self::expectExceptionMessage('MongoDB client encryption options are not set in configuration');
139+
$c->getClientEncryptionOptions();
140+
}
141+
142+
public function testKmsProviderNameRequired(): void
143+
{
144+
$c = new Configuration();
145+
self::expectException(ConfigurationException::class);
146+
self::expectExceptionMessage('The KMS provider "name" is required.');
147+
148+
// @phpstan-ignore argument.type
149+
$c->setKmsProvider(['foo' => 'bar']);
150+
}
151+
152+
public function testKmsProviderNameMustBeString(): void
153+
{
154+
$c = new Configuration();
155+
self::expectException(ConfigurationException::class);
156+
self::expectExceptionMessage('The KMS provider "name" must be a non-empty string.');
157+
158+
// @phpstan-ignore argument.type
159+
$c->setKmsProvider(['name' => ['not', 'a', 'string']]);
160+
}
131161
}

0 commit comments

Comments
 (0)