- Moving Notify things into "enum" class

- Adding \Countable interfaces to both UglyQueue and UglyQueueManager
This commit is contained in:
2014-10-30 10:54:06 -05:00
parent d20106a021
commit b840f40a89
5 changed files with 66 additions and 28 deletions

View File

@@ -3,6 +3,8 @@ ugly-queue
A simple file-based queue system for PHP 5.3.3+ A simple file-based queue system for PHP 5.3.3+
Build status: [![Build Status](https://travis-ci.org/dcarbone/ugly-queue.svg)](https://travis-ci.org/dcarbone/ugly-queue) Build statuses:
- master: [![Build Status](https://travis-ci.org/dcarbone/ugly-queue.svg?branch=master)](https://travis-ci.org/dcarbone/ugly-queue)
- 0.3.0: [![Build Status](https://travis-ci.org/dcarbone/ugly-queue.svg?branch=0.3.0)](https://travis-ci.org/dcarbone/ugly-queue)
Documentation and Test suites forthcoming. Documentation and Test suites forthcoming.

View File

@@ -21,11 +21,11 @@
"require" : { "require" : {
"php" : ">=5.3.3", "php" : ">=5.3.3",
"dcarbone/helpers" : "6.1.*" "dcarbone/helpers" : "~6.1"
}, },
"require-dev" : { "require-dev" : {
"phpunit/phpunit": "4.1.*" "phpunit/phpunit": "~4.1"
}, },
"autoload" : { "autoload" : {

View File

@@ -10,16 +10,8 @@ use DCarbone\Helpers\FileHelper;
* @property string path * @property string path
* @property bool locked * @property bool locked
*/ */
class UglyQueue implements \Serializable, \SplSubject class UglyQueue implements \Serializable, \SplSubject, \Countable
{ {
const NOTIFY_QUEUE_INITIALIZED = 0;
const NOTIFY_QUEUE_LOCKED = 1;
const NOTIFY_QUEUE_FAILED_TO_LOCK = 2;
const NOTIFY_QUEUE_LOCKED_BY_OTHER_PROCESS = 3;
const NOTIFY_QUEUE_UNLOCKED = 4;
const NOTIFY_QUEUE_PROCESSING = 5;
const NOTIFY_QUEUE_REACHED_END = 6;
/** @var int */ /** @var int */
public $notifyStatus; public $notifyStatus;
@@ -109,7 +101,7 @@ HTML;
file_put_contents($uglyQueue->_path.'queue.txt', ''); file_put_contents($uglyQueue->_path.'queue.txt', '');
} }
$uglyQueue->notifyStatus = self::NOTIFY_QUEUE_INITIALIZED; $uglyQueue->notifyStatus = UglyQueueEnum::QUEUE_INITIALIZED;
$uglyQueue->notify(); $uglyQueue->notify();
return $uglyQueue; return $uglyQueue;
@@ -169,7 +161,7 @@ HTML;
// If we make it this far, there is already a lock in place. // If we make it this far, there is already a lock in place.
$this->_locked = false; $this->_locked = false;
$this->notifyStatus = self::NOTIFY_QUEUE_LOCKED_BY_OTHER_PROCESS; $this->notifyStatus = UglyQueueEnum::QUEUE_LOCKED_BY_OTHER_PROCESS;
$this->notify(); $this->notify();
return false; return false;
@@ -187,14 +179,14 @@ HTML;
if ($ok !== true) if ($ok !== true)
{ {
$this->notifyStatus = self::NOTIFY_QUEUE_FAILED_TO_LOCK; $this->notifyStatus = UglyQueueEnum::QUEUE_FAILED_TO_LOCK;
$this->notify(); $this->notify();
return $this->_locked = false; return $this->_locked = false;
} }
$this->_locked = true; $this->_locked = true;
$this->notifyStatus = self::NOTIFY_QUEUE_LOCKED; $this->notifyStatus = UglyQueueEnum::QUEUE_LOCKED;
$this->notify(); $this->notify();
return true; return true;
@@ -210,7 +202,7 @@ HTML;
unlink($this->_path.'queue.lock'); unlink($this->_path.'queue.lock');
$this->_locked = false; $this->_locked = false;
$this->notifyStatus = self::NOTIFY_QUEUE_UNLOCKED; $this->notifyStatus = UglyQueueEnum::QUEUE_UNLOCKED;
$this->notify(); $this->notify();
} }
} }
@@ -270,9 +262,9 @@ HTML;
if ($count <= 0) if ($count <= 0)
throw new \InvalidArgumentException('Argument 1 expected to be integer greater than 0, "'.$count.'" seen'); throw new \InvalidArgumentException('Argument 1 expected to be integer greater than 0, "'.$count.'" seen');
if ($this->notifyStatus !== self::NOTIFY_QUEUE_PROCESSING) if ($this->notifyStatus !== UglyQueueEnum::QUEUE_PROCESSING)
{ {
$this->notifyStatus = self::NOTIFY_QUEUE_PROCESSING; $this->notifyStatus = UglyQueueEnum::QUEUE_PROCESSING;
$this->notify(); $this->notify();
} }
@@ -308,7 +300,7 @@ HTML;
ftruncate($queueFileHandle, 0); ftruncate($queueFileHandle, 0);
fclose($queueFileHandle); fclose($queueFileHandle);
$this->notifyStatus = self::NOTIFY_QUEUE_REACHED_END; $this->notifyStatus = UglyQueueEnum::QUEUE_REACHED_END;
$this->notify(); $this->notify();
} }
// Otherwise, create new queue file minus the processed lines. // Otherwise, create new queue file minus the processed lines.
@@ -428,6 +420,18 @@ HTML;
return false; return false;
} }
/**
* (PHP 5 >= 5.1.0)
* Count elements of an object
* @link http://php.net/manual/en/countable.count.php
*
* @return int The custom count as an integer.
*/
public function count()
{
return $this->getQueueItemCount();
}
/** /**
* (PHP 5 >= 5.1.0) * (PHP 5 >= 5.1.0)
* String representation of object * String representation of object

24
src/UglyQueueEnum.php Normal file
View File

@@ -0,0 +1,24 @@
<?php namespace DCarbone;
/**
* Class UglyQueueEnum
* @package DCarbone
*
* Pseudo-enum thing.
*/
abstract class UglyQueueEnum
{
// Typically used by UglyQueueManager
const MANAGER_INITIALIZED = 1;
const QUEUE_ADDED = 2;
const QUEUE_REMOVED = 3;
// Typically used by UglyQueues
const QUEUE_INITIALIZED = 100;
const QUEUE_LOCKED = 101;
const QUEUE_FAILED_TO_LOCK = 102;
const QUEUE_LOCKED_BY_OTHER_PROCESS = 103;
const QUEUE_UNLOCKED = 104;
const QUEUE_PROCESSING = 105;
const QUEUE_REACHED_END = 106;
}

View File

@@ -4,12 +4,8 @@
* Class UglyQueueManager * Class UglyQueueManager
* @package DCarbone * @package DCarbone
*/ */
class UglyQueueManager implements \SplObserver, \SplSubject class UglyQueueManager implements \SplObserver, \SplSubject, \Countable
{ {
const NOTIFY_MANAGER_INITIALIZED = 0;
const NOTIFY_QUEUE_ADDED = 1;
const NOTIFY_QUEUE_REMOVED = 2;
/** @var int */ /** @var int */
public $notifyStatus; public $notifyStatus;
@@ -75,7 +71,7 @@ class UglyQueueManager implements \SplObserver, \SplSubject
$manager->addQueue($uglyQueue); $manager->addQueue($uglyQueue);
} }
$manager->notifyStatus = self::NOTIFY_MANAGER_INITIALIZED; $manager->notifyStatus = UglyQueueEnum::MANAGER_INITIALIZED;
$manager->notify(); $manager->notify();
return $manager; return $manager;
@@ -93,7 +89,7 @@ class UglyQueueManager implements \SplObserver, \SplSubject
$this->queues[$uglyQueue->name] = $uglyQueue; $this->queues[$uglyQueue->name] = $uglyQueue;
$this->notifyStatus = self::NOTIFY_QUEUE_ADDED; $this->notifyStatus = UglyQueueEnum::QUEUE_ADDED;
$this->notify(); $this->notify();
return $this; return $this;
@@ -131,7 +127,7 @@ class UglyQueueManager implements \SplObserver, \SplSubject
if ($this->containsQueueWithName($name)) if ($this->containsQueueWithName($name))
{ {
unset($this->queues[$name]); unset($this->queues[$name]);
$this->notifyStatus = self::NOTIFY_QUEUE_REMOVED; $this->notifyStatus = UglyQueueEnum::QUEUE_REMOVED;
$this->notify(); $this->notify();
} }
@@ -168,6 +164,18 @@ class UglyQueueManager implements \SplObserver, \SplSubject
return array_keys($this->queues); return array_keys($this->queues);
} }
/**
* (PHP 5 >= 5.1.0)
* Count elements of an object
* @link http://php.net/manual/en/countable.count.php
*
* @return int The custom count as an integer. The return value is cast to an integer.
*/
public function count()
{
return count($this->queues);
}
/** /**
* (PHP 5 >= 5.1.0) * (PHP 5 >= 5.1.0)
* Receive update from subject * Receive update from subject