diff --git a/README.md b/README.md index 6ab32ab..a2e5f3c 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ ugly-queue 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. diff --git a/composer.json b/composer.json index 45e26b7..dcf5ac1 100644 --- a/composer.json +++ b/composer.json @@ -21,11 +21,11 @@ "require" : { "php" : ">=5.3.3", - "dcarbone/helpers" : "6.1.*" + "dcarbone/helpers" : "~6.1" }, "require-dev" : { - "phpunit/phpunit": "4.1.*" + "phpunit/phpunit": "~4.1" }, "autoload" : { diff --git a/src/UglyQueue.php b/src/UglyQueue.php index a0fe956..0337b99 100644 --- a/src/UglyQueue.php +++ b/src/UglyQueue.php @@ -10,16 +10,8 @@ use DCarbone\Helpers\FileHelper; * @property string path * @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 */ public $notifyStatus; @@ -109,7 +101,7 @@ HTML; file_put_contents($uglyQueue->_path.'queue.txt', ''); } - $uglyQueue->notifyStatus = self::NOTIFY_QUEUE_INITIALIZED; + $uglyQueue->notifyStatus = UglyQueueEnum::QUEUE_INITIALIZED; $uglyQueue->notify(); return $uglyQueue; @@ -169,7 +161,7 @@ HTML; // If we make it this far, there is already a lock in place. $this->_locked = false; - $this->notifyStatus = self::NOTIFY_QUEUE_LOCKED_BY_OTHER_PROCESS; + $this->notifyStatus = UglyQueueEnum::QUEUE_LOCKED_BY_OTHER_PROCESS; $this->notify(); return false; @@ -187,14 +179,14 @@ HTML; if ($ok !== true) { - $this->notifyStatus = self::NOTIFY_QUEUE_FAILED_TO_LOCK; + $this->notifyStatus = UglyQueueEnum::QUEUE_FAILED_TO_LOCK; $this->notify(); return $this->_locked = false; } $this->_locked = true; - $this->notifyStatus = self::NOTIFY_QUEUE_LOCKED; + $this->notifyStatus = UglyQueueEnum::QUEUE_LOCKED; $this->notify(); return true; @@ -210,7 +202,7 @@ HTML; unlink($this->_path.'queue.lock'); $this->_locked = false; - $this->notifyStatus = self::NOTIFY_QUEUE_UNLOCKED; + $this->notifyStatus = UglyQueueEnum::QUEUE_UNLOCKED; $this->notify(); } } @@ -270,9 +262,9 @@ HTML; if ($count <= 0) 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(); } @@ -308,7 +300,7 @@ HTML; ftruncate($queueFileHandle, 0); fclose($queueFileHandle); - $this->notifyStatus = self::NOTIFY_QUEUE_REACHED_END; + $this->notifyStatus = UglyQueueEnum::QUEUE_REACHED_END; $this->notify(); } // Otherwise, create new queue file minus the processed lines. @@ -428,6 +420,18 @@ HTML; 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) * String representation of object diff --git a/src/UglyQueueEnum.php b/src/UglyQueueEnum.php new file mode 100644 index 0000000..7c40209 --- /dev/null +++ b/src/UglyQueueEnum.php @@ -0,0 +1,24 @@ +addQueue($uglyQueue); } - $manager->notifyStatus = self::NOTIFY_MANAGER_INITIALIZED; + $manager->notifyStatus = UglyQueueEnum::MANAGER_INITIALIZED; $manager->notify(); return $manager; @@ -93,7 +89,7 @@ class UglyQueueManager implements \SplObserver, \SplSubject $this->queues[$uglyQueue->name] = $uglyQueue; - $this->notifyStatus = self::NOTIFY_QUEUE_ADDED; + $this->notifyStatus = UglyQueueEnum::QUEUE_ADDED; $this->notify(); return $this; @@ -131,7 +127,7 @@ class UglyQueueManager implements \SplObserver, \SplSubject if ($this->containsQueueWithName($name)) { unset($this->queues[$name]); - $this->notifyStatus = self::NOTIFY_QUEUE_REMOVED; + $this->notifyStatus = UglyQueueEnum::QUEUE_REMOVED; $this->notify(); } @@ -168,6 +164,18 @@ class UglyQueueManager implements \SplObserver, \SplSubject 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) * Receive update from subject