Skip to content

Commit 8e294e8

Browse files
committed
refactor: creating one exception file per class
Signed-off-by: rahul <[email protected]>
1 parent 3e2531e commit 8e294e8

8 files changed

+51
-70
lines changed

src/Exception/CouldNotWriteFileException.php

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

src/Exception/FileNotClosedException.php renamed to src/Exception/FileEncryptorException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use Exception;
66
use Throwable;
77

8-
class FileNotClosedException extends Exception
8+
class FileEncryptorException extends Exception
99
{
10-
public function __construct($message = "Failed to close file", $code = 0, Throwable $previous = null)
10+
public function __construct($message = "could not encrypt file", $code = 0, Throwable $previous = null)
1111
{
1212
parent::__construct($message, $code, $previous);
1313
}

src/Exception/FileNotFoundException.php renamed to src/Exception/FileHandlerException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use Exception;
66
use Throwable;
77

8-
class FileNotFoundException extends Exception
8+
class FileHandlerException extends Exception
99
{
10-
public function __construct($message = "File not found", $code = 0, Throwable $previous = null)
10+
public function __construct($message = "There was an error", $code = 0, Throwable $previous = null)
1111
{
1212
parent::__construct($message, $code, $previous);
1313
}

src/Exception/InvalidFileException.php

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

src/FileEncryptor.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,33 @@
33
namespace rcsofttech85\FileHandler;
44

55
use Exception;
6-
use rcsofttech85\FileHandler\Exception\FileNotFoundException;
6+
use rcsofttech85\FileHandler\Exception\FileEncryptorException;
77
use SensitiveParameter;
88
use SodiumException;
99

10-
class FileEncryptor
10+
readonly class FileEncryptor
1111
{
1212
public function __construct(
13-
private readonly string $filename,
14-
#[SensitiveParameter] private readonly string $secret
13+
private string $filename,
14+
#[SensitiveParameter] private string $secret
1515
) {
1616
}
1717

1818
/**
19-
* @throws SodiumException
19+
*
20+
* @throws FileEncryptorException
2021
* @throws Exception
22+
*
2123
*/
2224
public function encryptFile(): bool
2325
{
2426
$plainText = file_get_contents($this->filename);
2527

2628
if (!$plainText) {
27-
throw new FileNotFoundException('File not found or has no content');
29+
throw new FileEncryptorException('File not found or has no content');
2830
}
2931
if (ctype_xdigit($plainText)) {
30-
throw new SodiumException('file is already encrypted');
32+
throw new FileEncryptorException('file is already encrypted');
3133
}
3234

3335

@@ -53,18 +55,21 @@ public function encryptFile(): bool
5355
}
5456

5557
/**
58+
*
59+
* @throws FileEncryptorException
5660
* @throws SodiumException
61+
*
5762
*/
5863
public function decryptFile(): bool
5964
{
6065
$encryptedData = file_get_contents($this->filename);
6166

6267
if (!$encryptedData) {
63-
throw new FileNotFoundException('File not found or has no content');
68+
throw new FileEncryptorException('File not found or has no content');
6469
}
6570

6671
if (!ctype_xdigit($encryptedData)) {
67-
throw new SodiumException('file is not encrypted');
72+
throw new FileEncryptorException('file is not encrypted');
6873
}
6974

7075
$bytes = hex2bin($encryptedData);
@@ -76,7 +81,7 @@ public function decryptFile(): bool
7681
$plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
7782

7883
if (!$plaintext) {
79-
throw new SodiumException('could not decrypt file');
84+
throw new FileEncryptorException('could not decrypt file');
8085
}
8186

8287

src/FileHandler.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
namespace rcsofttech85\FileHandler;
44

55
use Generator;
6-
use rcsofttech85\FileHandler\Exception\CouldNotWriteFileException;
7-
use rcsofttech85\FileHandler\Exception\FileNotClosedException;
8-
use rcsofttech85\FileHandler\Exception\FileNotFoundException;
9-
use rcsofttech85\FileHandler\Exception\InvalidFileException;
6+
use rcsofttech85\FileHandler\Exception\FileHandlerException;
107

118
class FileHandler
129
{
@@ -15,7 +12,7 @@ class FileHandler
1512
private array $files = [];
1613

1714
/**
18-
* @throws FileNotFoundException
15+
* @throws FileHandlerException
1916
*/
2017
public function open(
2118
string $filename,
@@ -26,7 +23,7 @@ public function open(
2623
$file = fopen($filename, $mode, $include_path, $context);
2724

2825
if (!$file) {
29-
throw new FileNotFoundException();
26+
throw new FileHandlerException('File not found');
3027
}
3128

3229
$this->files[] = $file;
@@ -36,7 +33,7 @@ public function open(
3633

3734

3835
/**
39-
* @throws CouldNotWriteFileException
36+
* @throws FileHandlerException
4037
*/
4138
public function write(string $data, ?int $length = null): void
4239
{
@@ -46,18 +43,18 @@ public function write(string $data, ?int $length = null): void
4643
continue;
4744
}
4845

49-
throw new CouldNotWriteFileException();
46+
throw new FileHandlerException('Error writing to file');
5047
}
5148
}
5249

5350
/**
54-
* @throws FileNotClosedException
51+
* @throws FileHandlerException
5552
*/
5653
public function close(): void
5754
{
5855
foreach ($this->files as $file) {
5956
if (!fclose($file)) {
60-
throw new FileNotClosedException();
57+
throw new FileHandlerException('file was not closed');
6158
}
6259
}
6360
}
@@ -68,7 +65,7 @@ public function searchInCsvFile(string $keyword, string $column, string|null $fo
6865
}
6966

7067
/**
71-
* @throws InvalidFileException
68+
* @throws FileHandlerException
7269
*/
7370
public function toArray(): array
7471
{
@@ -77,7 +74,7 @@ public function toArray(): array
7774

7875

7976
/**
80-
* @throws InvalidFileException
77+
* @throws FileHandlerException
8178
*/
8279
public function toJson(): string
8380
{
@@ -86,13 +83,22 @@ public function toJson(): string
8683
return json_encode($data);
8784
}
8885

86+
public function delete()
87+
{
88+
foreach ($this->files as $file) {
89+
if (!unlink($file)) {
90+
throw new FileHandlerException('could not delete file');
91+
}
92+
}
93+
}
94+
8995
/**
90-
* @throws InvalidFileException
96+
* @throws FileHandlerException
9197
*/
9298
private function getRows(): Generator
9399
{
94100
if (count($this->files) > 1) {
95-
throw new InvalidFileException("multiple files not allowed");
101+
throw new FileHandlerException("multiple files not allowed");
96102
}
97103

98104
$file = $this->files[0];
@@ -110,13 +116,13 @@ private function getRows(): Generator
110116
fclose($file);
111117

112118
if ($isEmptyFile) {
113-
throw new InvalidFileException('invalid file format');
119+
throw new FileHandlerException('invalid file format');
114120
}
115121
}
116122

117123

118124
/**
119-
* @throws InvalidFileException
125+
* @throws FileHandlerException
120126
*/
121127
private function search(string $keyword, string $column, string|null $format): bool|array
122128
{
@@ -129,12 +135,12 @@ private function search(string $keyword, string $column, string|null $format): b
129135
}
130136

131137
/**
132-
* @throws InvalidFileException
138+
* @throws FileHandlerException
133139
*/
134140
private function isValidCsvFileFormat(array|false $row): void
135141
{
136142
if (!$row || count($row) <= 1) {
137-
throw new InvalidFileException('invalid file format');
143+
throw new FileHandlerException('invalid file format');
138144
}
139145
}
140146
}

tests/unit/FileEncryptorTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use PHPUnit\Framework\Attributes\Test;
66
use PHPUnit\Framework\TestCase;
7+
use rcsofttech85\FileHandler\Exception\FileEncryptorException;
78
use rcsofttech85\FileHandler\FileEncryptor;
8-
use SodiumException;
99
use Symfony\Component\Dotenv\Dotenv;
1010

1111
class FileEncryptorTest extends TestCase
@@ -38,7 +38,7 @@ public static function tearDownAfterClass(): void
3838
#[Test]
3939
public function throwExceptionOnDecryptingNonEncryptedFile()
4040
{
41-
$this->expectException(SodiumException::class);
41+
$this->expectException(FileEncryptorException::class);
4242
$this->expectExceptionMessage('file is not encrypted');
4343
$this->fileEncryptor->decryptFile();
4444
}
@@ -54,7 +54,7 @@ public function canEncryptFile()
5454
#[Test]
5555
public function throwExceptionIfAlreadyEncrypted()
5656
{
57-
$this->expectException(SodiumException::class);
57+
$this->expectException(FileEncryptorException::class);
5858
$this->expectExceptionMessage('file is already encrypted');
5959
$this->fileEncryptor->encryptFile();
6060
}
@@ -63,7 +63,7 @@ public function throwExceptionIfAlreadyEncrypted()
6363
public function throwExceptionIfDecryptionFails()
6464
{
6565
$this->fileEncryptor = new FileEncryptor("movie.csv", 'wrongSecret');
66-
$this->expectException(SodiumException::class);
66+
$this->expectException(FileEncryptorException::class);
6767
$this->expectExceptionMessage('could not decrypt file');
6868
$this->fileEncryptor->decryptFile();
6969
}

tests/unit/FileHandlerTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
use PHPUnit\Framework\Attributes\Test;
77
use PHPUnit\Framework\Attributes\TestDox;
88
use PHPUnit\Framework\TestCase;
9-
use rcsofttech85\FileHandler\Exception\CouldNotWriteFileException;
10-
use rcsofttech85\FileHandler\Exception\FileNotFoundException;
11-
use rcsofttech85\FileHandler\Exception\InvalidFileException;
9+
use rcsofttech85\FileHandler\Exception\FileHandlerException;
1210
use rcsofttech85\FileHandler\FileHandler;
1311
use TypeError;
1412

@@ -54,7 +52,7 @@ public function fileSuccessfullyWritten()
5452
#[Test]
5553
public function shouldThrowExceptionIfFileIsNotFound()
5654
{
57-
$this->expectException(FileNotFoundException::class);
55+
$this->expectException(FileHandlerException::class);
5856
$this->expectExceptionMessage('File not found');
5957
$this->fileHandler->open(filename: 'unknown');
6058
}
@@ -64,7 +62,7 @@ public function shouldThrowExceptionIfFileIsNotWritable()
6462
{
6563
$this->fileHandler->open(filename: 'file', mode: 'r');
6664

67-
$this->expectException(CouldNotWriteFileException::class);
65+
$this->expectException(FileHandlerException::class);
6866
$this->expectExceptionMessage('Error writing to file');
6967
$this->fileHandler->write(data: "hello world");
7068
}
@@ -180,7 +178,7 @@ public function searchByKeywordAndReturnArray()
180178
#[DataProvider('fileProvider')]
181179
public function throwErrorIfFileFormatIsInvalid(string $file)
182180
{
183-
$this->expectException(InvalidFileException::class);
181+
$this->expectException(FileHandlerException::class);
184182
$this->expectExceptionMessage('invalid file format');
185183

186184
try {

0 commit comments

Comments
 (0)