Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cassani committed Nov 14, 2017
1 parent 55e63d9 commit 3757a73
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 42 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Simple Locker Manager

[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/mauretto78/locker-manager/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/mauretto78/locker-manager/?branch=master)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/b0278e2b5b9b4feb8f9078326d3721fd)](https://www.codacy.com/app/mauretto78/locker-manager?utm_source=github.com&utm_medium=referral&utm_content=mauretto78/locker-manager&utm_campaign=Badge_Grade)
[![license](https://img.shields.io/github/license/mauretto78/simple-event-store-manager.svg)]()
[![Packagist](https://img.shields.io/packagist/v/mauretto78/simple-event-store-manager.svg)]()

This library is suitable for you if you need to simple lock system.

## Installation
Expand Down Expand Up @@ -54,14 +59,14 @@ $lock = new Lock(
$lockerManager->acquire($lock);

// get a lock
$sampleLock = $lockerManager->get('sample-lock');
$sampleLock = $lockerManager->get('Sample Lock');

// delete a lock
$lockerManager->delete('sample-lock');
$lockerManager->delete('Sample Lock');

// update a lock
$lockerManager->update(
'sample-lock',
'Sample Lock',
[
'name' => 'Maria Dante',
'email' => 'maria.dante@gmail.com',
Expand Down
8 changes: 8 additions & 0 deletions src/Application/LockerManager.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* This file is part of the LockerManager package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace LockerManager\Application;

Expand Down
11 changes: 9 additions & 2 deletions src/Domain/Lock.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* This file is part of the LockerManager package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace LockerManager\Domain;

Expand Down Expand Up @@ -40,8 +48,7 @@ class Lock
public function __construct(
$key,
$payload
)
{
) {
$this->id = Uuid::uuid4();
$this->key = $key;
$this->payload = $payload;
Expand Down
10 changes: 9 additions & 1 deletion src/Infrastructure/Exception/ExistingKeyException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?php
/**
* This file is part of the LockerManager package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace LockerManager\Infrastructure\Exception;

class ExistingKeyException extends \Exception
{
}
}
10 changes: 9 additions & 1 deletion src/Infrastructure/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?php
/**
* This file is part of the LockerManager package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace LockerManager\Infrastructure\Exception;

class InvalidArgumentException extends \InvalidArgumentException
{
}
}
10 changes: 9 additions & 1 deletion src/Infrastructure/Exception/LockingKeyException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?php
/**
* This file is part of the LockerManager package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace LockerManager\Infrastructure\Exception;

class LockingKeyException extends \Exception
{
}
}
10 changes: 9 additions & 1 deletion src/Infrastructure/Exception/NotExistingKeyException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
<?php
/**
* This file is part of the LockerManager package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace LockerManager\Infrastructure\Exception;

class NotExistingKeyException extends \Exception
{
}
}
33 changes: 21 additions & 12 deletions src/Infrastructure/FLockerStore.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<?php
/**
* This file is part of the LockerManager package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace LockerManager\Infrastructure;

use Cocur\Slugify\Slugify;
use LockerManager\Domain\Lock;
use LockerManager\Infrastructure\Exception\ExistingKeyException;
use LockerManager\Infrastructure\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -45,7 +54,7 @@ public function acquire(Lock $lock)
$key = $lock->key();
$fileName = $this->getLockPath($key);

if(file_exists($fileName)){
if (file_exists($fileName)) {
throw new ExistingKeyException(sprintf('The key "%s" already exists.', $key));
}

Expand All @@ -59,11 +68,11 @@ public function acquire(Lock $lock)
*/
private function save(Lock $lock, $fileName)
{
$file = @fopen($fileName,'w');
$file = @fopen($fileName, 'w');

if (flock($file,LOCK_EX)) {
if (flock($file, LOCK_EX)) {
fwrite($file, serialize($lock));
flock($file,LOCK_UN);
flock($file, LOCK_UN);
} else {
throw new LockingKeyException(sprintf('Error locking file "%s".', $lock->key()));
}
Expand All @@ -78,8 +87,8 @@ public function clear()
{
$files = scandir($this->lockPath);

foreach($files as $file){
if(is_file($this->lockPath.$file)) {
foreach ($files as $file) {
if (is_file($this->lockPath.$file)) {
unlink($this->lockPath.$file);
}
}
Expand All @@ -91,7 +100,7 @@ public function clear()
*/
public function delete($key)
{
if(!$this->exists($key)){
if (!$this->exists($key)) {
throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key));
}

Expand All @@ -104,7 +113,7 @@ public function delete($key)
*/
public function exists($key)
{
if(!@fopen($this->getLockPath($key),'r')){
if (!@fopen($this->getLockPath($key), 'r')) {
return false;
}

Expand All @@ -117,7 +126,7 @@ public function exists($key)
*/
private function getLockPath($key)
{
return $this->lockPath.$key.'.lock';
return $this->lockPath.(new Slugify())->slugify($key).'.lock';
}

/**
Expand All @@ -127,7 +136,7 @@ private function getLockPath($key)
*/
public function get($key)
{
if(!$this->exists($key)){
if (!$this->exists($key)) {
throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key));
}

Expand All @@ -145,7 +154,7 @@ public function getAll()
unset($files[0]);
unset($files[1]);

foreach ($files as $lock){
foreach ($files as $lock) {
$locks[] = str_replace('.lock', '', $lock);
}

Expand All @@ -162,7 +171,7 @@ public function update($key, $payload)
{
$fileName = $this->getLockPath($key);

if(!file_exists($fileName)){
if (!file_exists($fileName)) {
throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key));
}

Expand Down
8 changes: 8 additions & 0 deletions src/Infrastructure/LockerStoreInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<?php
/**
* This file is part of the LockerManager package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace LockerManager\Infrastructure;

Expand Down
36 changes: 26 additions & 10 deletions src/Infrastructure/RedisLockerStore.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
<?php
/**
* This file is part of the LockerManager package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace LockerManager\Infrastructure;

use Cocur\Slugify\Slugify;
use LockerManager\Domain\Lock;
use LockerManager\Infrastructure\Exception\ExistingKeyException;
use LockerManager\Infrastructure\Exception\NotExistingKeyException;
Expand Down Expand Up @@ -33,7 +42,7 @@ public function acquire(Lock $lock)
{
$key = $lock->key();

if($this->redis->hget(self::LOCK_LIST_NAME, $key)){
if ($this->redis->hget(self::LOCK_LIST_NAME, $key)) {
throw new ExistingKeyException(sprintf('The key "%s" already exists.', $key));
}

Expand All @@ -43,6 +52,15 @@ public function acquire(Lock $lock)
);
}

/**
* @param $key
* @return string
*/
private function getLockPath($key)
{
return (new Slugify())->slugify($key);
}

/**
* @param $key
* @param Lock $lock
Expand All @@ -51,7 +69,7 @@ private function saveLock($key, Lock $lock)
{
$this->redis->hset(
self::LOCK_LIST_NAME,
$key,
$this->getLockPath($key),
serialize($lock)
);
}
Expand All @@ -70,11 +88,11 @@ public function clear()
*/
public function delete($key)
{
if(!$this->exists($key)){
if (!$this->exists($key)) {
throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key));
}

$this->redis->hdel(self::LOCK_LIST_NAME, $key);
$this->redis->hdel(self::LOCK_LIST_NAME, $this->getLockPath($key));
}

/**
Expand All @@ -83,7 +101,7 @@ public function delete($key)
*/
public function exists($key)
{
return ($this->redis->hget(self::LOCK_LIST_NAME, $key)) ? true : false;
return ($this->redis->hget(self::LOCK_LIST_NAME, $this->getLockPath($key))) ? true : false;
}

/**
Expand All @@ -93,11 +111,11 @@ public function exists($key)
*/
public function get($key)
{
if(!$this->exists($key)){
if (!$this->exists($key)) {
throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key));
}

return unserialize($this->redis->hget(self::LOCK_LIST_NAME, $key));
return unserialize($this->redis->hget(self::LOCK_LIST_NAME, $this->getLockPath($key)));
}

/**
Expand All @@ -115,7 +133,7 @@ public function getAll()
*/
public function update($key, $payload)
{
if(!$this->redis->hget(self::LOCK_LIST_NAME, $key)){
if (!$this->redis->hget(self::LOCK_LIST_NAME, $this->getLockPath($key))) {
throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key));
}

Expand All @@ -128,6 +146,4 @@ public function update($key, $payload)
$lock
);
}


}
Loading

0 comments on commit 3757a73

Please sign in to comment.