Skip to content

Commit fc26c70

Browse files
committed
simplify checks, add custom Exceptions
1 parent 1b44d84 commit fc26c70

File tree

4 files changed

+69
-45
lines changed

4 files changed

+69
-45
lines changed

src/Environment.php

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace MessageQueue;
1010

1111

12+
use Exception\FileCreateError;
1213
use Symfony\Component\OptionsResolver\OptionsResolver;
1314

1415
class Environment
@@ -67,104 +68,89 @@ public function rotateAmount()
6768
public function create()
6869
{
6970
$queueDir = $this->queueDir();
70-
if (!is_dir($queueDir)) {
71-
if (!@mkdir($queueDir)) {
72-
throw new \ErrorException(error_get_last());
73-
}
71+
if (!@mkdir($queueDir, 0775, true) && !is_dir($queueDir)) {
72+
throw new FileCreateError(error_get_last());
7473
}
7574
$readFile = $this->readFile();
76-
if (!is_file($readFile)) {
77-
if (!@touch($readFile)) {
78-
throw new \ErrorException(error_get_last());
79-
}
75+
if (!is_file($readFile) && !@touch($readFile)) {
76+
throw new FileCreateError(error_get_last());
8077
}
8178
$readPointerFile = $this->rotateFile();
82-
if (!is_file($readPointerFile)) {
83-
if (!@touch($readPointerFile)) {
84-
throw new \ErrorException(error_get_last());
85-
}
79+
if (!is_file($readPointerFile) && !@touch($readPointerFile)) {
80+
throw new FileCreateError(error_get_last());
8681
}
8782
$writeFile = $this->writeFile();
88-
if (!is_file($writeFile)) {
89-
if (!@touch($writeFile)) {
90-
throw new \ErrorException(error_get_last());
91-
}
83+
if (!is_file($writeFile) && !@touch($writeFile)) {
84+
throw new FileCreateError(error_get_last());
9285
}
9386
}
9487

9588
public function reset()
9689
{
97-
$fp = fopen($this->readFile(), "r+");
90+
$fp = fopen($this->readFile(), "br+");
9891
ftruncate($fp, 0);
9992
fclose($fp);
10093

101-
$fp = fopen($this->rotateFile(), "r+");
94+
$fp = fopen($this->rotateFile(), "br+");
10295
ftruncate($fp, 0);
10396
fclose($fp);
10497

105-
$fp = fopen($this->writeFile(), "r+");
98+
$fp = fopen($this->writeFile(), "br+");
10699
ftruncate($fp, 0);
107100
fclose($fp);
108101
}
109102

110103
public function remove()
111104
{
112-
$dir = $this->options['dir'];
113105
$readFile = $this->readFile();
114-
if (is_file($readFile)) {
115-
if (!@unlink($readFile)) {
116-
throw new \ErrorException(error_get_last());
117-
}
106+
if (is_file($readFile) && !@unlink($readFile)) {
107+
throw new FileCreateError(error_get_last());
118108
}
119109
$readPointerFile = $this->rotateFile();
120-
if (is_file($readPointerFile)) {
121-
if (!@unlink($readPointerFile)) {
122-
throw new \ErrorException(error_get_last());
123-
}
110+
if (is_file($readPointerFile) && !@unlink($readPointerFile)) {
111+
throw new FileCreateError(error_get_last());
124112
}
113+
125114
$writeFile = $this->writeFile();
126-
if (is_file($writeFile)) {
127-
if (!@unlink($writeFile)) {
128-
throw new \ErrorException(error_get_last());
129-
}
115+
if (is_file($writeFile) && !@unlink($writeFile)) {
116+
throw new FileCreateError(error_get_last());
130117
}
118+
131119
$queueDir = $this->queueDir();
132-
if (is_dir($queueDir)) {
133-
if (!@rmdir($queueDir)) {
134-
throw new \ErrorException(error_get_last());
135-
}
120+
if (is_dir($queueDir) && !@rmdir($queueDir)) {
121+
throw new FileCreateError(error_get_last());
136122
}
137123
}
138124

139125
public function validate()
140126
{
141127
$queueDir = $this->queueDir();
142128
if (!is_dir($queueDir)) {
143-
throw new \ErrorException('Queue Directory not created');
129+
throw new FileCreateError('Queue Directory not created');
144130
}
145131
if (!is_writable($queueDir)) {
146-
throw new \ErrorException('Queue Directory not writable');
132+
throw new FileCreateError('Queue Directory not writable');
147133
}
148134
$readFile = $this->readFile();
149135
if (!is_file($readFile)) {
150-
throw new \ErrorException('Read file not created');
136+
throw new FileCreateError('Read file not created');
151137
}
152138
if (!is_writable($readFile)) {
153-
throw new \ErrorException('Read file not writable');
139+
throw new FileCreateError('Read file not writable');
154140
}
155141
$readPointerFile = $this->rotateFile();
156142
if (!is_file($readPointerFile)) {
157-
throw new \ErrorException('Read pointer file not created');
143+
throw new FileCreateError('Read pointer file not created');
158144
}
159145
if (!is_writable($readPointerFile)) {
160-
throw new \ErrorException('Read pointer file not writable');
146+
throw new FileCreateError('Read pointer file not writable');
161147
}
162148
$writeFile = $this->writeFile();
163149
if (!is_file($writeFile)) {
164-
throw new \ErrorException('Write file not created');
150+
throw new FileCreateError('Write file not created');
165151
}
166152
if (!is_writable($writeFile)) {
167-
throw new \ErrorException('Write file not writable');
153+
throw new FileCreateError('Write file not writable');
168154
}
169155
}
170156
}

src/Exception/FileAccessError.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Exception;
4+
5+
6+
class FileAccessError extends \RuntimeException
7+
{
8+
}

src/Exception/FileCreateError.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Exception;
4+
5+
6+
class FileCreateError extends \RuntimeException
7+
{
8+
}

src/Queue.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public function write(array $messages)
2323
return;
2424
}
2525
$f = fopen($this->env->writeFile(), 'a');
26+
if(false === $f) {
27+
throw new \RuntimeException('Unable to open file for write "ab" '.$this->env->writeFile());
28+
}
2629
flock($f, LOCK_EX);
2730
foreach($messages as $message) {
2831
fwrite($f, (string) $message . PHP_EOL);
@@ -34,6 +37,10 @@ public function write(array $messages)
3437
public function rotate(int $amount)
3538
{
3639
$f = fopen($this->env->rotateFile(), 'r+');
40+
41+
if(false === $f) {
42+
throw new \RuntimeException('Unable to rotate file for write "br+" '.$this->env->writeFile());
43+
}
3744
flock($f, LOCK_EX);
3845

3946
$seek = fgets($f);
@@ -43,6 +50,9 @@ public function rotate(int $amount)
4350

4451

4552
$f2 = fopen($this->env->writeFile(), 'r');
53+
if(false === $f2) {
54+
throw new \RuntimeException('Unable to open file for rotate "br" '.$this->env->writeFile());
55+
}
4656
fseek($f2, $seek);
4757

4858
$lines = [];
@@ -55,6 +65,9 @@ public function rotate(int $amount)
5565
}
5666

5767
$f3 = fopen($this->env->readFile(), 'a');
68+
if(false === $f3) {
69+
throw new \RuntimeException('Unable to open file for rotate "ba" '.$this->env->writeFile());
70+
}
5871
flock($f3, LOCK_EX);
5972

6073
foreach(array_reverse($lines) as $line) {
@@ -93,6 +106,9 @@ public function read(int $amount) : array
93106
public function recycle()
94107
{
95108
$f = fopen($this->env->rotateFile(), 'r+');
109+
if(false === $f) {
110+
throw new \RuntimeException('Unable to open file for write "ab" '.$this->env->writeFile());
111+
}
96112
flock($f, LOCK_EX);
97113

98114
$seek = fgets($f);
@@ -104,6 +120,9 @@ public function recycle()
104120

105121

106122
$f2 = fopen($this->env->writeFile(), 'r+');
123+
if(false === $f2) {
124+
throw new \RuntimeException('Unable to open file for write "ab" '.$this->env->writeFile());
125+
}
107126
flock($f2, LOCK_EX);
108127
fseek($f2, $seek);
109128

@@ -136,6 +155,9 @@ public function recycle()
136155
private function messages(int $amount) : array
137156
{
138157
$f = fopen($this->env->readFile(), 'r+');
158+
if(false === $f) {
159+
throw new \RuntimeException('Unable to open file for messages "br+" '.$this->env->writeFile());
160+
}
139161
flock($f, LOCK_EX);
140162

141163
$pos = -1;
@@ -180,4 +202,4 @@ function($line) {
180202
$lines
181203
);
182204
}
183-
}
205+
}

0 commit comments

Comments
 (0)