Skip to content

simplify checks, add custom Exceptions #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 30 additions & 44 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace MessageQueue;


use Exception\FileCreateError;
use Symfony\Component\OptionsResolver\OptionsResolver;

class Environment
Expand Down Expand Up @@ -67,104 +68,89 @@ public function rotateAmount()
public function create()
{
$queueDir = $this->queueDir();
if (!is_dir($queueDir)) {
if (!@mkdir($queueDir)) {
throw new \ErrorException(error_get_last());
}
if (!@mkdir($queueDir, 0775, true) && !is_dir($queueDir)) {
throw new FileCreateError(error_get_last());
}
$readFile = $this->readFile();
if (!is_file($readFile)) {
if (!@touch($readFile)) {
throw new \ErrorException(error_get_last());
}
if (!is_file($readFile) && !@touch($readFile)) {
throw new FileCreateError(error_get_last());
}
$readPointerFile = $this->rotateFile();
if (!is_file($readPointerFile)) {
if (!@touch($readPointerFile)) {
throw new \ErrorException(error_get_last());
}
if (!is_file($readPointerFile) && !@touch($readPointerFile)) {
throw new FileCreateError(error_get_last());
}
$writeFile = $this->writeFile();
if (!is_file($writeFile)) {
if (!@touch($writeFile)) {
throw new \ErrorException(error_get_last());
}
if (!is_file($writeFile) && !@touch($writeFile)) {
throw new FileCreateError(error_get_last());
}
}

public function reset()
{
$fp = fopen($this->readFile(), "r+");
$fp = fopen($this->readFile(), "br+");
ftruncate($fp, 0);
fclose($fp);

$fp = fopen($this->rotateFile(), "r+");
$fp = fopen($this->rotateFile(), "br+");
ftruncate($fp, 0);
fclose($fp);

$fp = fopen($this->writeFile(), "r+");
$fp = fopen($this->writeFile(), "br+");
ftruncate($fp, 0);
fclose($fp);
}

public function remove()
{
$dir = $this->options['dir'];
$readFile = $this->readFile();
if (is_file($readFile)) {
if (!@unlink($readFile)) {
throw new \ErrorException(error_get_last());
}
if (is_file($readFile) && !@unlink($readFile)) {
throw new FileCreateError(error_get_last());
}
$readPointerFile = $this->rotateFile();
if (is_file($readPointerFile)) {
if (!@unlink($readPointerFile)) {
throw new \ErrorException(error_get_last());
}
if (is_file($readPointerFile) && !@unlink($readPointerFile)) {
throw new FileCreateError(error_get_last());
}

$writeFile = $this->writeFile();
if (is_file($writeFile)) {
if (!@unlink($writeFile)) {
throw new \ErrorException(error_get_last());
}
if (is_file($writeFile) && !@unlink($writeFile)) {
throw new FileCreateError(error_get_last());
}

$queueDir = $this->queueDir();
if (is_dir($queueDir)) {
if (!@rmdir($queueDir)) {
throw new \ErrorException(error_get_last());
}
if (is_dir($queueDir) && !@rmdir($queueDir)) {
throw new FileCreateError(error_get_last());
}
}

public function validate()
{
$queueDir = $this->queueDir();
if (!is_dir($queueDir)) {
throw new \ErrorException('Queue Directory not created');
throw new FileCreateError('Queue Directory not created');
}
if (!is_writable($queueDir)) {
throw new \ErrorException('Queue Directory not writable');
throw new FileCreateError('Queue Directory not writable');
}
$readFile = $this->readFile();
if (!is_file($readFile)) {
throw new \ErrorException('Read file not created');
throw new FileCreateError('Read file not created');
}
if (!is_writable($readFile)) {
throw new \ErrorException('Read file not writable');
throw new FileCreateError('Read file not writable');
}
$readPointerFile = $this->rotateFile();
if (!is_file($readPointerFile)) {
throw new \ErrorException('Read pointer file not created');
throw new FileCreateError('Read pointer file not created');
}
if (!is_writable($readPointerFile)) {
throw new \ErrorException('Read pointer file not writable');
throw new FileCreateError('Read pointer file not writable');
}
$writeFile = $this->writeFile();
if (!is_file($writeFile)) {
throw new \ErrorException('Write file not created');
throw new FileCreateError('Write file not created');
}
if (!is_writable($writeFile)) {
throw new \ErrorException('Write file not writable');
throw new FileCreateError('Write file not writable');
}
}
}
8 changes: 8 additions & 0 deletions src/Exception/FileAccessError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Exception;


class FileAccessError extends \RuntimeException
{
}
8 changes: 8 additions & 0 deletions src/Exception/FileCreateError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Exception;


class FileCreateError extends \RuntimeException
{
}
24 changes: 23 additions & 1 deletion src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public function write(array $messages)
return;
}
$f = fopen($this->env->writeFile(), 'a');
if(false === $f) {
throw new \RuntimeException('Unable to open file for write "ab" '.$this->env->writeFile());
}
flock($f, LOCK_EX);
foreach($messages as $message) {
fwrite($f, (string) $message . PHP_EOL);
Expand All @@ -34,6 +37,10 @@ public function write(array $messages)
public function rotate(int $amount)
{
$f = fopen($this->env->rotateFile(), 'r+');

if(false === $f) {
throw new \RuntimeException('Unable to rotate file for write "br+" '.$this->env->writeFile());
}
flock($f, LOCK_EX);

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


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

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

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

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

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


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

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

$pos = -1;
Expand Down Expand Up @@ -180,4 +202,4 @@ function($line) {
$lines
);
}
}
}