Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- */
- namespace SWIFT\Library;
- use SWIFT\Library\Exception;
- /**
- * The lock class is used to prevent race conditions in tasks (recurring etc.).
- *
- * @author Varun Shoor <varun.shoor@kayako.com>
- */
- class Lock
- {
- const DEFAULT_EXPIRY = 30; // in minutes
- // string
- protected $name;
- /**
- * @author Varun Shoor
- *
- * @param string $name
- */
- public function __construct($name)
- {
- $this->setName($name);
- }
- /**
- * @return Lock
- * @throws Exception\InvalidArgument
- */
- protected function setName($name)
- {
- if (empty($name)) {
- throw new Exception\InvalidArgument;
- }
- $this->name = $name;
- }
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- *
- * @return string
- * @todo name should be returned instead of 'this'
- */
- public function getName()
- {
- return $this;
- }
- /**
- * Attempts to create a new lock, if one already exists, returns false
- *
- * @author Varun Shoor <varun.shoor@kayako.com>
- * @todo realease the lock, implement file time modification based on expiry
- *
- * @param string $name
- * @param int $expiry (OPTIONAL) In minutes
- *
- * @return Lock
- * @throws Exception\InvalidArgument
- */
- static public function create($name, $expiry = 0)
- {
- $name = clean($name);
- if (empty($name)) {
- throw new Exception\InvalidArgument;
- } // If the lock already exists, we need to check whether it has expired
- elseif (self::exists($name)) {
- $Lock = new self($name);
- // If the lock has expired, we need to release it before proceeding
- if (!$Lock->HasExpired()) {
- /*
- * @todo Release the lock here
- */
- } // Apparently the lock is still active, we cant proceed!
- else {
- return null;
- }
- }
- if (empty($expiry)) {
- $expiry = time() + (60 * self::DEFAULT_EXPIRY);
- }
- file_put_contents(self::getCacheName($name), self::getContents($expiry));
- /*
- * @todo Implement file time modification based on expiry
- */
- return new self($name);
- }
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- *
- * @param string $name
- *
- * @return boolean
- */
- static public function exists($name)
- {
- $name = clean($name);
- if (empty($name)) {
- throw new Exception\InvalidArgument;
- }
- $cache = new self($name);
- if (!$cache->getJSON()) {
- return false;
- }
- $cache->increment();
- return true;
- }
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- *
- * @param \stdClass $json
- *
- * @return Lock
- */
- public function update(\stdClass $json)
- {
- file_put_contents(self::getCacheName($this->getName()), json_encode($json));
- return $this;
- }
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- *
- * @return Lock
- */
- function increment()
- {
- $json = $this->getJSON();
- if (!$json) {
- return $this;
- }
- // Extend the expiry
- $json->expiry = $json->expiry + (60 * self::DEFAULT_EXPIRY);
- $json->hits++;
- return $this->update($json);
- }
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- *
- * @return object
- */
- public function ReleaseTheLock()
- {
- if (!self::exists($this->getName())) {
- return $this;
- }
- unlink(self::getCacheName($this->getName()));
- return $this;
- }
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- *
- * @return int|false
- */
- public function getExpiry()
- {
- return $this->getValue('expiry');
- }
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- *
- * @return int|false
- */
- public function getHits()
- {
- return $this->getValue('hits');
- }
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- *
- * @return object
- */
- public function getJSON()
- {
- $json = file_get_contents(self::getCacheName($this->getName()));
- // JSON decoding failed? We will need to clean things up
- if ($json == false) {
- return null;
- }
- return (object) $json;
- }
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- *
- * @param string $name
- *
- * @return string|false
- * @throws Exception\InvalidArgument
- */
- function getValue($name)
- {
- if (empty($name)) {
- throw new Exception\InvalidArgument;
- }
- $json = $this->getJSON();
- if (!$json || !isset($json->{$name})) {
- return false;
- }
- return $json->{$name};
- }
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- *
- * @return boolean
- */
- function HasExpired()
- {
- $expiry = $this->getExpiry();
- if ($expiry === false || $expiry < NOW) {
- $this->ReleaseTheLock();
- return true;
- }
- return false;
- }
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- *
- * @return json
- */
- public function __toString()
- {
- return json_encode ($this->getJSON(), JSON_PRETTY_PRINT);
- }
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- *
- * @param string $name
- *
- * @return string
- */
- static protected function getCacheName($name)
- {
- return 'lock.' . $name;
- }
- /**
- * @author Varun Shoor <varun.shoor@kayako.com>
- *
- * @param integer $expiry
- * @param integer $hits
- *
- * @return object
- */
- static protected function getContents($expiry, $hits = 1)
- {
- return (object) array(
- 'expiry' => $expiry,
- 'hits' => $hits
- );
- }
- }
- ?>
Add Comment
Please, Sign In to add comment