Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Commit

Permalink
Allow Symfony ^4.4 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Verhoeven authored Jun 9, 2020
1 parent 47a96ed commit 812e84e
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 94 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
/vendor/
composer.lock
.DS_Store
.idea
.phpcs-cache
.phpunit.result.cache
12 changes: 7 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
language: php

php:
- '7.0'
- '7.1'
- '7.2'
- '7.2'
- '7.3'
- '7.4'

before_script:
- composer self-update
- composer install --prefer-source
- composer self-update
- composer install --prefer-source
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"description": "Stash storage implementation for the Symfony Lock component",
"type": "library",
"require": {
"php": "^7.0",
"symfony/lock": "^3.4",
"php": "^7.2",
"symfony/lock": "^3.4 || ^4.4",
"tedivm/stash": "^0.15"
},
"require-dev": {
"phpunit/phpunit": "~4.8"
"phpunit/phpunit": "^8.5",
"myonlinestore/coding-standard": "^2.0",
"vimeo/psalm": "^3.11"
},
"license": "MIT",
"authors": [
Expand All @@ -19,7 +21,9 @@
],
"minimum-stability": "stable",
"autoload": {
"psr-4": { "Sandwich\\Symfony\\Lock\\Store\\": "src" }
"psr-4": {
"Sandwich\\Symfony\\Lock\\Store\\": "src"
}
},
"autoload-dev": {
"psr-4": {
Expand Down
16 changes: 16 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"
>
<arg name="basepath" value="."/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>
<arg value="sp"/>

<file>src</file>
<file>tests</file>

<rule ref="MyOnlineStore"/>
</ruleset>
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
>
<testsuites>
<testsuite name="Sandwich Test Suite">
<directory>./tests/</directory>
Expand Down
15 changes: 15 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
60 changes: 19 additions & 41 deletions src/StashStore.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Sandwich\Symfony\Lock\Store;

Expand All @@ -8,38 +9,28 @@
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\StoreInterface;

/** @psalm-suppress DeprecatedInterface */
final class StashStore implements StoreInterface
{
/**
* @var PoolInterface
*/
/** @var PoolInterface */
private $pool;

/**
* @var int
*/
/** @var int */
private $initialTtl;

/**
* @param PoolInterface $pool
* @param int $initialTtl
*/
public function __construct(PoolInterface $pool, $initialTtl = 300)
public function __construct(PoolInterface $pool, int $initialTtl = 300)
{
if ($initialTtl < 1) {
throw new InvalidArgumentException(
sprintf('%s() expects a strictly positive TTL. Got %d.', __METHOD__, $initialTtl)
\sprintf('%s() expects a strictly positive TTL. Got %d.', __METHOD__, $initialTtl)
);
}

$this->pool = $pool;
$this->initialTtl = $initialTtl;
}

/**
* @inheritdoc
*/
public function save(Key $key)
public function save(Key $key): void
{
$item = $this->pool->getItem((string) $key);

Expand All @@ -55,24 +46,21 @@ public function save(Key $key)
$this->putOffExpiration($key, $this->initialTtl);
}

/**
* @inheritdoc
*/
public function waitAndSave(Key $key)
public function waitAndSave(Key $key): void
{
throw new InvalidArgumentException(
sprintf('The store "%s" does not supports blocking locks.', get_class($this))
\sprintf('The store "%s" does not supports blocking locks.', static::class)
);
}

/**
* @inheritdoc
*/
public function putOffExpiration(Key $key, $ttl)
public function putOffExpiration(Key $key, $ttl): void
{
if ($ttl < 1) {
throw new InvalidArgumentException(
sprintf('%s() expects a TTL greater or equals to 1. Got %s.', __METHOD__, $ttl)
\sprintf('%s() expects a TTL greater or equals to 1. Got %s.', __METHOD__, $ttl)
);
}

Expand All @@ -84,17 +72,14 @@ public function putOffExpiration(Key $key, $ttl)
}

$item->set($token);
$item->setTTL((int) ceil($ttl));
$item->setTTL((int) \ceil($ttl));

if (!$item->save()) {
throw new LockConflictedException();
}
}

/**
* @inheritdoc
*/
public function delete(Key $key)
public function delete(Key $key): void
{
$item = $this->pool->getItem((string) $key);

Expand All @@ -110,10 +95,7 @@ public function delete(Key $key)
$this->pool->deleteItem((string) $key);
}

/**
* @inheritdoc
*/
public function exists(Key $key)
public function exists(Key $key): bool
{
$item = $this->pool->getItem((string) $key);

Expand All @@ -122,18 +104,14 @@ public function exists(Key $key)

/**
* Retrieve an unique token for the given key.
*
* @param Key $key
*
* @return string
*/
private function getToken(Key $key)
private function getToken(Key $key): string
{
if (!$key->hasState(__CLASS__)) {
$token = base64_encode(random_bytes(32));
$key->setState(__CLASS__, $token);
if (!$key->hasState(self::class)) {
$token = \base64_encode(\random_bytes(32));
$key->setState(self::class, $token);
}

return $key->getState(__CLASS__);
return $key->getState(self::class);
}
}
Loading

0 comments on commit 812e84e

Please sign in to comment.