Skip to content

Commit 40128d3

Browse files
committed
- Uses the KodedException from koded/stdlib
- Removes version from composer.json
1 parent 819f656 commit 40128d3

3 files changed

Lines changed: 21 additions & 8 deletions

File tree

Processors/File.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Koded\Logging\Processors;
44

55
use Exception;
6+
use Koded\Exceptions\KodedException;
67

78
/**
89
* Log processor for storing the log messages into text files on the disk.
@@ -22,6 +23,10 @@
2223
class File extends Processor
2324
{
2425

26+
const E_DIRECTORY_DOES_NOT_EXIST = 1;
27+
const E_DIRECTORY_NOT_WRITABLE = 2;
28+
const E_DIRECTORY_NOT_CREATED = 3;
29+
2530
/**
2631
* @var string The log filename.
2732
*/
@@ -52,19 +57,19 @@ protected function initialize(array $settings)
5257
) . DIRECTORY_SEPARATOR;
5358

5459
if (!is_dir($dir)) {
55-
throw new Exception(sprintf('Log directory "%s" must exist.', $dir));
60+
throw new FileProcessorException(self::E_DIRECTORY_DOES_NOT_EXIST, [':dir' => $dir]);
5661
}
5762

5863
if (!is_writable($dir)) {
59-
throw new Exception(sprintf('Log directory "%s" must be writable.', $dir));
64+
throw new FileProcessorException(self::E_DIRECTORY_NOT_WRITABLE, [':dir' => $dir]);
6065
}
6166

6267
$dir = sprintf('%s%s%s', $dir, date('Y/m'), DIRECTORY_SEPARATOR);
6368

6469
if (!is_dir($dir)) {
6570
if (false === @mkdir($dir, 0777, true)) {
6671
// @codeCoverageIgnoreStart
67-
throw new Exception(sprintf('Failed to create a log directory "%s".', $dir));
72+
throw new FileProcessorException(self::E_DIRECTORY_NOT_CREATED, [':dir' => $dir]);
6873
// @codeCoverageIgnoreEnd
6974
}
7075
}
@@ -77,3 +82,12 @@ protected function parse(array $message)
7782
file_put_contents($this->filename, strtr($this->format, $message) . PHP_EOL, FILE_APPEND);
7883
}
7984
}
85+
86+
class FileProcessorException extends KodedException
87+
{
88+
protected $messages = [
89+
File::E_DIRECTORY_DOES_NOT_EXIST => 'Log directory ":dir" must exist.',
90+
File::E_DIRECTORY_NOT_WRITABLE => 'Log directory ":dir" must be writable.',
91+
File::E_DIRECTORY_NOT_CREATED => 'Failed to create a log directory ":dir".',
92+
];
93+
}

Tests/Processors/FileTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Koded\Logging\Processors;
44

5-
use Exception;
65
use org\bovigo\vfs\vfsStream;
76
use org\bovigo\vfs\vfsStreamDirectory;
87
use org\bovigo\vfs\vfsStreamWrapper;
@@ -42,7 +41,7 @@ public function testWhenDirectoryDoesNotExist($message)
4241
{
4342
$dir = $this->dir->url() . '/nonexistent';
4443

45-
$this->expectException(Exception::class);
44+
$this->expectException(FileProcessorException::class);
4645
$this->expectExceptionMessage('Log directory "' . $dir . '/" must exist');
4746

4847
$processor = new File(['dir' => $dir]);
@@ -57,7 +56,7 @@ public function testWhenDirectoryIsNotWritable($message)
5756
{
5857
$dir = $this->dir->url();
5958

60-
$this->expectException(Exception::class);
59+
$this->expectException(FileProcessorException::class);
6160
$this->expectExceptionMessage('Log directory "' . $dir . '/" must be writable');
6261

6362
vfsStreamWrapper::getRoot()->chmod(0400);

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"name": "koded/logging",
3-
"version": "1.0.3",
43
"type": "library",
54
"license": "BSD-3-Clause",
65
"description": "A simple standalone logging facility with several log processors",
@@ -13,7 +12,8 @@
1312
],
1413
"require": {
1514
"php": "~7",
16-
"psr/log": "^1.0.1"
15+
"psr/log": "^1.0.1",
16+
"koded/stdlib": "~1"
1717
},
1818
"autoload": {
1919
"psr-4": {

0 commit comments

Comments
 (0)