Skip to content

Commit a11b355

Browse files
committed
Use new generic exceptions
1 parent 168b986 commit a11b355

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

composer.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/OpenSSLEncryption.php

+13-9
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace PHPSess\Encryption;
66

7-
use PHPSess\Interfaces\EncryptionInterface;
7+
use PHPSess\Exception\UnableToEncryptException;
88
use PHPSess\Exception\UnableToDecryptException;
9-
use PHPSess\Exception\UnknownEncryptionAlgorithmException;
10-
use PHPSess\Exception\UnknownHashAlgorithmException;
9+
use PHPSess\Exception\UnableToHashException;
10+
use PHPSess\Interfaces\EncryptionInterface;
1111

1212
class OpenSSLEncryption implements EncryptionInterface
1313
{
@@ -135,16 +135,18 @@ private function getEncryptionKey(string $sessionId): string
135135
*
136136
* To get a list of valid algorithms, see openssl_get_cipher_methods(true)
137137
*
138-
* @throws UnknownEncryptionAlgorithmException
139-
* @param string $algorithm
138+
* @throws UnableToEncryptException
139+
* @param string $algorithm For a list of valid algorithm, see openssl_get_cipher_methods(true).
140140
* @return void
141141
*/
142142
public function setEncryptionAlgorithm(string $algorithm): void
143143
{
144144
$knownAlgorithms = openssl_get_cipher_methods(true);
145145

146146
if (!in_array($algorithm, $knownAlgorithms)) {
147-
throw new UnknownEncryptionAlgorithmException();
147+
$errorMessage = "The encryption algorithm \"$algorithm\" is unknow. " .
148+
'For a list of valid algorithms, see openssl_get_cipher_methods(true).';
149+
throw new UnableToEncryptException($errorMessage);
148150
}
149151

150152
$this->encryptionAlgorithm = $algorithm;
@@ -155,16 +157,18 @@ public function setEncryptionAlgorithm(string $algorithm): void
155157
*
156158
* To get a list of valid algorithms, see openssl_get_md_methods(true)
157159
*
158-
* @throws UnknownHashAlgorithmException
159-
* @param string $algorithm
160+
* @throws UnableToHashException
161+
* @param string $algorithm For a list of valid algorithms, see openssl_get_md_methods(true).
160162
* @return void
161163
*/
162164
public function setHashAlgorithm(string $algorithm): void
163165
{
164166
$knownAlgorithms = openssl_get_md_methods(true);
165167

166168
if (!in_array($algorithm, $knownAlgorithms)) {
167-
throw new UnknownHashAlgorithmException();
169+
$errorMessage = "The hash algorithm \"$algorithm\" is unknown." .
170+
'For a list of valid algorithms, see openssl_get_md_methods(true).';
171+
throw new UnableToHashException($errorMessage);
168172
}
169173

170174
$this->hashAlgorithm = $algorithm;

tests/OpenSSLEncryptionTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace PHPSess\Tests;
66

77
use PHPSess\Encryption\OpenSSLEncryption;
8-
use PHPSess\Exception\UnknownHashAlgorithmException;
9-
use PHPSess\Exception\UnknownEncryptionAlgorithmException;
8+
use PHPSess\Exception\UnableToHashException;
9+
use PHPSess\Exception\UnableToEncryptException;
1010
use PHPSess\Exception\UnableToDecryptException;
1111

1212
use PHPUnit\Framework\TestCase;
@@ -18,7 +18,7 @@ public function testThrowErrorUnknownHash()
1818
{
1919
$encryption = new OpenSSLEncryption('appKey');
2020

21-
$this->expectException(UnknownHashAlgorithmException::class);
21+
$this->expectException(UnableToHashException::class);
2222

2323
$encryption->setHashAlgorithm('unknown_hash_algo');
2424
}
@@ -27,7 +27,7 @@ public function testThrowErrorUnknownEncryption()
2727
{
2828
$encryption = new OpenSSLEncryption('appKey');
2929

30-
$this->expectException(UnknownEncryptionAlgorithmException::class);
30+
$this->expectException(UnableToEncryptException::class);
3131

3232
$encryption->setEncryptionAlgorithm('unknown_encryption_algo');
3333
}

0 commit comments

Comments
 (0)