Skip to content

Commit 8654590

Browse files
authored
Removed wrap_exceptions configuration setting (absolute-quantum#65)
1 parent 06393db commit 8654590

File tree

10 files changed

+8
-151
lines changed

10 files changed

+8
-151
lines changed

TODO-6.0.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/DependencyInjection/Configuration.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ public function getConfigTreeBuilder(): TreeBuilder
4444
->scalarNode('secret')
4545
->defaultValue(null)
4646
->end()
47-
->scalarNode('wrap_exceptions')
48-
->defaultFalse()
49-
->end()
5047
->end();
5148

5249
return $treeBuilder;

src/DependencyInjection/DoctrineEncryptExtension.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,6 @@
1919
*/
2020
class DoctrineEncryptExtension extends Extension
2121
{
22-
/**
23-
* Flag to test if we should wrap exceptions by our own exceptions.
24-
*
25-
* @internal
26-
*/
27-
public static $wrapExceptions = false;
28-
2922
public const SupportedEncryptorClasses = [
3023
'Defuse' => DefuseEncryptor::class,
3124
'Halite' => HaliteEncryptor::class,
@@ -82,20 +75,6 @@ public function load(array $configs, ContainerBuilder $container): void
8275
} else {
8376
$loader->load('service_listeners_with_attributes.yml');
8477
}
85-
86-
// Wrap exceptions
87-
if ($config['wrap_exceptions']) {
88-
self::$wrapExceptions = true;
89-
} else {
90-
trigger_deprecation(
91-
'doctrineencryptbundle/doctrine-encrypt-bundle',
92-
'5.4.2',
93-
<<<'EOF'
94-
Starting from 6.0, all exceptions thrown by this library will be wrapped by \Ambta\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException or a child-class of it.
95-
You can start using these exceptions today by setting 'ambta_doctrine_encrypt.wrap_exceptions' to TRUE.
96-
EOF
97-
);
98-
}
9978
}
10079

10180
/**

src/Encryptors/DefuseEncryptor.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Ambta\DoctrineEncryptBundle\Encryptors;
66

7-
use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension;
87
use Ambta\DoctrineEncryptBundle\Exception\UnableToDecryptException;
98
use Ambta\DoctrineEncryptBundle\Exception\UnableToEncryptException;
109

@@ -35,10 +34,7 @@ public function encrypt(string $data): string
3534
try {
3635
return \Defuse\Crypto\Crypto::encryptWithPassword($data, $this->secret);
3736
} catch (\Throwable $e) {
38-
if (DoctrineEncryptExtension::$wrapExceptions) {
39-
throw new UnableToEncryptException($e->getMessage(), $e->getCode(), $e);
40-
}
41-
throw $e;
37+
throw new UnableToEncryptException($e->getMessage(), $e->getCode(), $e);
4238
}
4339
}
4440

@@ -51,10 +47,7 @@ public function decrypt(string $data): string
5147
try {
5248
return \Defuse\Crypto\Crypto::decryptWithPassword($data, $this->secret);
5349
} catch (\Throwable $e) {
54-
if (DoctrineEncryptExtension::$wrapExceptions) {
55-
throw new UnableToDecryptException($e->getMessage(), $e->getCode(), $e);
56-
}
57-
throw $e;
50+
throw new UnableToDecryptException($e->getMessage(), $e->getCode(), $e);
5851
}
5952
}
6053
}

src/Encryptors/HaliteEncryptor.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Ambta\DoctrineEncryptBundle\Encryptors;
66

7-
use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension;
87
use Ambta\DoctrineEncryptBundle\Exception\UnableToDecryptException;
98
use Ambta\DoctrineEncryptBundle\Exception\UnableToEncryptException;
109
use ParagonIE\Halite\KeyFactory;
@@ -40,10 +39,7 @@ public function encrypt(string $data): string
4039
try {
4140
return Crypto::encrypt(new HiddenString($data), $this->getKey());
4241
} catch (\Throwable $e) {
43-
if (DoctrineEncryptExtension::$wrapExceptions) {
44-
throw new UnableToEncryptException($e->getMessage(), $e->getCode(), $e);
45-
}
46-
throw $e;
42+
throw new UnableToEncryptException($e->getMessage(), $e->getCode(), $e);
4743
}
4844
}
4945

@@ -58,10 +54,7 @@ public function decrypt(string $data): string
5854
try {
5955
return Crypto::decrypt($data, $this->getKey())->getString();
6056
} catch (\Throwable $e) {
61-
if (DoctrineEncryptExtension::$wrapExceptions) {
62-
throw new UnableToDecryptException($e->getMessage(), $e->getCode(), $e);
63-
}
64-
throw $e;
57+
throw new UnableToDecryptException($e->getMessage(), $e->getCode(), $e);
6558
}
6659
}
6760

src/Resources/doc/upgrading.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Upgrading to 6.x
22
## Breaking changes
3+
* Remove the wrap_exceptions configuration setting if it was used before updating as the cache:clear would fail
34
* Instead of exceptions directly from halite or defuse, you now get a `\DoctrineEncryptCommunity\DoctrineEncryptBundle\Exception\UnableToEncryptException`
45
or a `\DoctrineEncryptCommunity\DoctrineEncryptBundle\Exception\UnableToDecryptException`, which both extend `\DoctrineEncryptCommunity\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException`.
56
* Throw a `\DoctrineEncryptCommunity\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException` in case something goes wrong encrypting/decrypting

src/Subscribers/DoctrineEncryptSubscriber.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Ambta\DoctrineEncryptBundle\Subscribers;
44

5-
use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension;
65
use Ambta\DoctrineEncryptBundle\Encryptors\EncryptorInterface;
76
use Ambta\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException;
87
use Ambta\DoctrineEncryptBundle\Mapping\AttributeReader;
@@ -329,10 +328,7 @@ public function processFields(object $entity, EntityManagerInterface $entityMana
329328
} catch (DoctrineEncryptBundleException $e) {
330329
throw $e;
331330
} catch (\Throwable $e) {
332-
if (DoctrineEncryptExtension::$wrapExceptions) {
333-
throw new DoctrineEncryptBundleException('Something went wrong encrypting/decrypting a secret', 0, $e);
334-
}
335-
throw $e;
331+
throw new DoctrineEncryptBundleException('Something went wrong encrypting/decrypting a secret', 0, $e);
336332
}
337333

338334
return $entity;

tests/Unit/DependencyInjection/DoctrineEncryptExtensionTest.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -204,46 +204,6 @@ public function testSecretsAreReadFromFile(): void
204204
static::assertEquals($expectedSecret, $actualSecret);
205205
}
206206

207-
/**
208-
* @group legacy
209-
*/
210-
public function testWrapExceptionsTriggersDeprecationWarningWhenNotDefiningTheOption(): void
211-
{
212-
$container = $this->createContainer();
213-
$config = [];
214-
215-
$this->expectDeprecation('Since doctrineencryptbundle/doctrine-encrypt-bundle 5.4.2: Starting from 6.0, all exceptions thrown by this library will be wrapped by \Ambta\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException or a child-class of it.
216-
You can start using these exceptions today by setting \'ambta_doctrine_encrypt.wrap_exceptions\' to TRUE.');
217-
$this->extension->load([$config], $container);
218-
$this->assertFalse(DoctrineEncryptExtension::$wrapExceptions);
219-
}
220-
221-
/**
222-
* @group legacy
223-
*/
224-
public function testWrapExceptionsTriggersDeprecationWarningWhenDisabled(): void
225-
{
226-
$container = $this->createContainer();
227-
$config = ['wrap_exceptions' => false];
228-
229-
$this->expectDeprecation('Since doctrineencryptbundle/doctrine-encrypt-bundle 5.4.2: Starting from 6.0, all exceptions thrown by this library will be wrapped by \Ambta\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException or a child-class of it.
230-
You can start using these exceptions today by setting \'ambta_doctrine_encrypt.wrap_exceptions\' to TRUE.');
231-
$this->extension->load([$config], $container);
232-
$this->assertFalse(DoctrineEncryptExtension::$wrapExceptions);
233-
}
234-
235-
/**
236-
* @group legacy
237-
*/
238-
public function testWrapExceptionsDoesNotTriggerDeprecationWarningWhenEnabled(): void
239-
{
240-
$container = $this->createContainer();
241-
$config = ['wrap_exceptions' => true];
242-
243-
$this->extension->load([$config], $container);
244-
$this->assertTrue(DoctrineEncryptExtension::$wrapExceptions);
245-
}
246-
247207
private function createContainer(): ContainerBuilder
248208
{
249209
$container = new ContainerBuilder(

tests/Unit/Encryptors/DefuseEncryptorTest.php

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,13 @@
22

33
namespace Ambta\DoctrineEncryptBundle\Tests\Unit\Encryptors;
44

5-
use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension;
65
use Ambta\DoctrineEncryptBundle\Encryptors\DefuseEncryptor;
76
use Ambta\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException;
87
use PHPUnit\Framework\TestCase;
98

109
class DefuseEncryptorTest extends TestCase
1110
{
1211
private const DATA = 'foobar';
13-
/** @var bool */
14-
private $originalWrapExceptions;
15-
16-
protected function setUp(): void
17-
{
18-
$this->originalWrapExceptions = DoctrineEncryptExtension::$wrapExceptions;
19-
}
20-
21-
protected function tearDown(): void
22-
{
23-
DoctrineEncryptExtension::$wrapExceptions = $this->originalWrapExceptions;
24-
}
2512

2613
public function testEncrypt(): void
2714
{
@@ -38,24 +25,8 @@ public function testEncrypt(): void
3825
static::assertSame($key, $newkey, 'The key must not be modified');
3926
}
4027

41-
public function testEncryptorThrowsOwnExceptionWhenExceptionsAreNotWrapped(): void
28+
public function testEncryptorThrowsBundleException(): void
4229
{
43-
DoctrineEncryptExtension::$wrapExceptions = false;
44-
45-
try {
46-
(new DefuseEncryptor('not-a-valid-key'))->decrypt('foo');
47-
48-
$this->fail('The encryptor should have thrown an error');
49-
} catch (\Throwable $e) {
50-
$this->assertNotInstanceOf(\PHPUnit\Framework\Exception::class, $e);
51-
$this->assertNotInstanceOf(DoctrineEncryptBundleException::class, $e);
52-
}
53-
}
54-
55-
public function testEncryptorThrowsBundleExceptionWhenExceptionsAreWrapped(): void
56-
{
57-
DoctrineEncryptExtension::$wrapExceptions = true;
58-
5930
try {
6031
(new DefuseEncryptor('not-a-valid-key'))->decrypt('foo');
6132

tests/Unit/Encryptors/HaliteEncryptorTest.php

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Ambta\DoctrineEncryptBundle\Tests\Unit\Encryptors;
44

5-
use Ambta\DoctrineEncryptBundle\DependencyInjection\DoctrineEncryptExtension;
65
use Ambta\DoctrineEncryptBundle\Encryptors\HaliteEncryptor;
76
use Ambta\DoctrineEncryptBundle\Exception\DoctrineEncryptBundleException;
87
use PHPUnit\Framework\TestCase;
@@ -11,19 +10,6 @@ class HaliteEncryptorTest extends TestCase
1110
{
1211
private const DATA = 'foobar';
1312

14-
/** @var bool */
15-
private $originalWrapExceptions;
16-
17-
protected function setUp(): void
18-
{
19-
$this->originalWrapExceptions = DoctrineEncryptExtension::$wrapExceptions;
20-
}
21-
22-
protected function tearDown(): void
23-
{
24-
DoctrineEncryptExtension::$wrapExceptions = $this->originalWrapExceptions;
25-
}
26-
2713
public function testEncryptExtension(): void
2814
{
2915
if (!extension_loaded('sodium') && !class_exists('ParagonIE_Sodium_Compat')) {
@@ -40,24 +26,8 @@ public function testEncryptExtension(): void
4026
static::assertSame(self::DATA, $decrypted);
4127
}
4228

43-
public function testEncryptorThrowsOwnExceptionWhenExceptionsAreNotWrapped(): void
44-
{
45-
DoctrineEncryptExtension::$wrapExceptions = false;
46-
47-
try {
48-
(new HaliteEncryptor('not-a-valid-key'))->encrypt('foo');
49-
50-
$this->fail('The encryptor should have thrown an error');
51-
} catch (\Throwable $e) {
52-
$this->assertNotInstanceOf(\PHPUnit\Framework\Exception::class, $e);
53-
$this->assertNotInstanceOf(DoctrineEncryptBundleException::class, $e);
54-
}
55-
}
56-
57-
public function testEncryptorThrowsBundleExceptionWhenExceptionsAreWrapped(): void
29+
public function testEncryptorThrowsBundleException(): void
5830
{
59-
DoctrineEncryptExtension::$wrapExceptions = true;
60-
6131
try {
6232
(new HaliteEncryptor('not-a-valid-key'))->encrypt('foo');
6333

0 commit comments

Comments
 (0)