Skip to content

Commit

Permalink
merging PDO branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cassani committed Nov 16, 2017
1 parent e4cf02e commit ca3fb7c
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 138 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
config/parameters.yml
build
var
vendor
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,45 @@ composer require mauretto78/locker-manager

### Instantiate LockerManager

To instantiate the LockerManager you must inject an implementation of `LockerStoreInterface`:
To instantiate the LockerManager you must inject an implementation of `LockerStoreInterface`. You can use:

* `FLockerStore`
* `PdoLockerStore`
* `RedisLockerStore`

Take a look:

```php
use LockerManager\Application\LockerManager;
use LockerManager\Infrastructure\FLockerStore;
use LockerManager\Infrastructure\PdoLockerStore;
use LockerManager\Infrastructure\RedisLockerStore;
use Predis\Client;

// 1. Redis implementation uses PRedis Client
$redisLockerStore = new RedisLockerStore(new Client());
$lockerManager = new LockerManager($redisLockerStore);

// 2. Filesystem implementation
// Filesystem implementation
$fLockerStore = new FLockerStore('var/lock/');
$lockerManager = new LockerManager($fLockerStore);

```

```php
// ..

// PDO implementation
$pdoLockerStore = new PdoLockerStore(new \PDO($config));
$lockerManager = new LockerManager($pdoLockerStore);

```

```php
// ..

// Redis implementation uses PRedis Client
$redisLockerStore = new RedisLockerStore(new Client($config));
$lockerManager = new LockerManager($redisLockerStore);

```

### Acquire, get, delete and update a lock

This library uses [Slugify](https://github.com/cocur/slugify) to save lock keys.
Expand Down
8 changes: 8 additions & 0 deletions config/parameters.dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pdo:
driver: 'mysql'
host: 'localhost'
username: 'root'
password: ~
database: 'locker'
port: '3306'
options: ~
8 changes: 8 additions & 0 deletions config/parameters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pdo:
driver: 'mysql'
host: '127.0.0.1'
username: 'root'
password: '@Mauretto78'
database: 'locker'
port: '3306'
options: ~
123 changes: 77 additions & 46 deletions src/Infrastructure/PdoLockerStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ private function createSchema()
{
$query = "CREATE TABLE IF NOT EXISTS `".self::LOCKERSTORE_TABLE_NAME."` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uuid` char(36) COLLATE utf8_unicode_ci NOT NULL COMMENT '(DC2Type:guid)',
`key` varchar(255) DEFAULT NULL,
`payload` varchar(255) DEFAULT NULL,
`created_at` datetime(6),
`modified_at` datetime(6),
`key` varchar(255) NOT NULL UNIQUE,
`body` text DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";

Expand All @@ -59,33 +56,48 @@ private function createSchema()
*/
public function acquire(Lock $lock)
{
$uuid = (string) $lock->id();
$key = $lock->key();
$payload = serialize($lock->payload());
$createdAt = $lock->createdAt()->format('Y-m-d H:i:s.u');
$modifiedAt = $lock->modifiedAt()->format('Y-m-d H:i:s.u');

$sql = 'INSERT INTO `'.self::LOCKERSTORE_TABLE_NAME.'` (
`uuid`,
`key`,
`payload`,
`created_at`,
`modified_at`
`body`
) VALUES (
:uuid,
:key,
:payload,
:created_at,
:modified_at
:body
)';

$stmt = $this->pdo->prepare($sql);
$stmt->bindParam(':uuid', $uuid);
$stmt->bindParam(':key', $key);
$stmt->bindParam(':payload', serialize($payload));
$stmt->bindParam(':created_at', $createdAt);
$stmt->bindParam(':modified_at', $modifiedAt);
$stmt->execute();
$data = [
'key' => $lock->key(),
'body' => serialize($lock)
];

$this->executeQueryInATransaction($sql, $data);
}

/**
* @param $sql
* @param array|null $data
*/
private function executeQueryInATransaction($sql, array $data = null)
{
try {
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE,\PDO::ERRMODE_EXCEPTION);
$this->pdo->beginTransaction();

$stmt = $this->pdo->prepare($sql);

if ($data) {
foreach ($data as $key => &$value){
$stmt->bindParam(':'.$key, $value);
}
}

$stmt->execute();

$this->pdo->commit();
} catch(\PDOException $e){
$this->pdo->rollBack();

throw $e;
}
}

/**
Expand All @@ -94,8 +106,8 @@ public function acquire(Lock $lock)
public function clear()
{
$sql = 'DELETE FROM `'.self::LOCKERSTORE_TABLE_NAME.'`';
$stmt = $this->pdo->prepare($sql);
$stmt->execute();

$this->executeQueryInATransaction($sql);
}

/**
Expand All @@ -110,9 +122,12 @@ public function delete($key)

$key = (new Slugify())->slugify($key);
$sql = 'DELETE FROM `'.self::LOCKERSTORE_TABLE_NAME.'` WHERE `key` = :key';
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam(':key', $key);
$stmt->execute();

$data = [
'key' => $key
];

$this->executeQueryInATransaction($sql, $data);
}

/**
Expand Down Expand Up @@ -143,21 +158,19 @@ public function get($key)

$key = (new Slugify())->slugify($key);
$query = 'SELECT
`id`,
`uuid`,
`key`,
`payload`,
`created_at`,
`modified_at`,
`body`
FROM `'.self::LOCKERSTORE_TABLE_NAME.'`
WHERE `key` = :key';
$stmt = $this->pdo->prepare($query);
$stmt->bindParam(':key', $key);
$stmt->execute();

$row = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$row = $stmt->fetchAll(
\PDO::FETCH_ASSOC
);

return $row[0];
return unserialize($row[0]['body']);
}

/**
Expand All @@ -166,22 +179,40 @@ public function get($key)
public function getAll()
{
$query = 'SELECT
`id`,
`uuid`,
`key`,
`payload`,
`created_at`,
`modified_at`,
FROM `'.self::LOCKERSTORE_TABLE_NAME.'`
ORDER BY `created_at` ASC';
`body`
FROM `'.self::LOCKERSTORE_TABLE_NAME.'`';
$stmt = $this->pdo->prepare($query);
$stmt->execute();

return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}

/**
* @param $key
* @param $payload
*
* @throws NotExistingKeyException
*/
public function update($key, $payload)
{
//UPDATE MyGuests SET lastname='Doe' WHERE id=2
if (!$this->exists($key)) {
throw new NotExistingKeyException(sprintf('The key "%s" does not exists.', $key));
}

/** @var Lock $lock */
$lock = $this->get($key);
$lock->update($payload);

$sql = "UPDATE `".self::LOCKERSTORE_TABLE_NAME."`
SET `body` = :lock
WHERE `key` = :key";

$data = [
'key' => $lock->key(),
'lock' => serialize($lock)
];

$this->executeQueryInATransaction($sql, $data);
}
}
Loading

0 comments on commit ca3fb7c

Please sign in to comment.