Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update composer-deps #41

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-redis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
matrix:
dependencies: ["lowest", "highest"]
php-version:
- "8.2"
- "8.3"
- "8.4"
operating-system: ["ubuntu-latest"]

steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
matrix:
dependencies: ["lowest", "highest"]
php-version:
- "8.2"
- "8.3"
- "8.4"
operating-system: ["ubuntu-latest"]

steps:
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
}
],
"require": {
"php": "^8.2",
"php": "^8.3",
"psr/clock": "^1.0"
},
"require-dev": {
"brainbits/phpcs-standard": "^7.0.1",
"brainbits/phpstan-rules": "^3.1",
"brainbits/phpstan-rules": "^4.0",
"matthiasnoback/symfony-config-test": "^5.1",
"matthiasnoback/symfony-dependency-injection-test": "^6.0",
"mikey179/vfsstream": "^1.6.11",
"phpstan/phpstan": "^1.10.67",
"phpunit/phpunit": "^11.3.6",
"phpstan/phpstan": "^2.1.1",
"phpunit/phpunit": "^11.5",
"predis/predis": "^2.2",
"symfony/clock": "^6.4|^7.0",
"symfony/config": "^6.4|^7.0",
Expand All @@ -30,8 +30,8 @@
"symfony/http-kernel": "^6.4|^7.0",
"symfony/routing": "^6.4|^7.0",
"symfony/security-core": "^6.4|^7.0",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-symfony": "^1.3"
"phpstan/phpstan-phpunit": "^2.0",
"phpstan/phpstan-symfony": "^2.0"
},
"suggest": {
"predis/predis": "If you want to use the PredisStorage",
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ parameters:
- src
bootstrapFiles:
- vendor/autoload.php
excludePaths:
# Declaring types for static function parameters is not possible
- src/Bundle/DependencyInjection/Configuration.php
includes:
- vendor/brainbits/phpstan-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
Expand Down
4 changes: 2 additions & 2 deletions src/Blocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Brainbits\Blocking;

use Brainbits\Blocking\Exception\BlockFailedException;
use Brainbits\Blocking\Exception\AlreadyBlockedException;
use Brainbits\Blocking\Identity\BlockIdentity;
use Brainbits\Blocking\Owner\OwnerFactoryInterface;
use Brainbits\Blocking\Storage\StorageInterface;
Expand All @@ -32,7 +32,7 @@ public function block(BlockIdentity $identifier, int|null $ttl = null): Block
$block = $this->tryBlock($identifier, $ttl);

if ($block === null) {
throw BlockFailedException::createAlreadyBlocked($identifier);
throw AlreadyBlockedException::createAlreadyBlocked($identifier);
}

return $block;
Expand Down
8 changes: 5 additions & 3 deletions src/Bundle/DependencyInjection/BrainbitsBlockingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

use function assert;
use function sprintf;

class BrainbitsBlockingExtension extends Extension
Expand All @@ -32,8 +31,11 @@ public function load(array $configs, ContainerBuilder $container): void
$yamlLoader->load('services.yaml');
$configuration = $this->getConfiguration($configs, $container);

assert($configuration instanceof Configuration);

/** @var array{
* storage: non-empty-array<string>,
* clock?: class-string,
* block_interval: int,
* owner_factory: array{value?: string, driver: string, service: string}} $config */
$config = $this->processConfiguration($configuration, $configs);

if (isset($config['storage']['predis'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use function sprintf;

class BlockFailedException extends RuntimeException
class AlreadyBlockedException extends RuntimeException
{
public static function createAlreadyBlocked(BlockIdentity $identity): self
{
Expand Down
4 changes: 0 additions & 4 deletions src/Owner/SymfonySessionOwnerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Brainbits\Blocking\Exception\NoSessionException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

final readonly class SymfonySessionOwnerFactory implements OwnerFactoryInterface
{
Expand All @@ -32,9 +31,6 @@ public function createOwner(): Owner
}

$session = $request->getSession();
if (!$session instanceof SessionInterface) {
throw NoSessionException::create();
}

return new Owner($session->getId());
}
Expand Down
30 changes: 18 additions & 12 deletions src/Storage/FilesystemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
use Brainbits\Blocking\Owner\Owner;
use DateTimeImmutable;
use Psr\Clock\ClockInterface;
use Throwable;

use function assert;
use function dirname;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function is_array;
use function is_string;
use function is_writable;
use function json_decode;
Expand Down Expand Up @@ -137,13 +137,16 @@ public function exists(BlockIdentity $identity): bool
$metaContent = file_get_contents($metaFilename);
assert(is_string($metaContent));
assert($metaContent !== '');
/** @var array{ttl: int, updatedAt: string} $metaData */
$metaData = json_decode($metaContent, true);
assert(is_array($metaData));

$now = $this->clock->now();

$expiresAt = (new DateTimeImmutable((string) $metaData['updatedAt'], $now->getTimezone()))
->modify('+' . $metaData['ttl'] . ' seconds');
try {
$expiresAt = (new DateTimeImmutable((string) $metaData['updatedAt'], $now->getTimezone()))
->modify('+' . $metaData['ttl'] . ' seconds');
} catch (Throwable) {
throw UnserializeFailedException::createFromInput($metaContent);
}

return $expiresAt > $now;
}
Expand All @@ -162,16 +165,19 @@ public function get(BlockIdentity $identity): Block|null
throw UnserializeFailedException::createFromInput($content);
}

/** @var array{identity: string, owner: string} $data */
$data = json_decode($content, true);

assert(is_array($data));
assert($data['identity'] ?? false);
assert($data['owner'] ?? false);
try {
$block = new Block(
new BlockIdentity($data['identity']),
new Owner($data['owner']),
);
} catch (Throwable) {
throw UnserializeFailedException::createFromInput($content);
}

return new Block(
new BlockIdentity($data['identity']),
new Owner($data['owner']),
);
return $block;
}

private function getFilename(BlockIdentity $identifier): string
Expand Down
21 changes: 12 additions & 9 deletions src/Storage/PredisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

use Brainbits\Blocking\Block;
use Brainbits\Blocking\Exception\IOException;
use Brainbits\Blocking\Exception\UnserializeFailedException;
use Brainbits\Blocking\Identity\BlockIdentity;
use Brainbits\Blocking\Owner\Owner;
use Predis\ClientInterface;
use Predis\PredisException;
use Throwable;

use function assert;
use function is_array;
use function json_decode;
use function json_encode;

Expand Down Expand Up @@ -110,16 +110,19 @@ public function get(BlockIdentity $identity): Block|null
throw IOException::getFailed((string) $identity);
}

/** @var array{identity: string, owner: string} $data */
$data = json_decode($content, true);

assert(is_array($data));
assert($data['identity'] ?? false);
assert($data['owner'] ?? false);
try {
$block = new Block(
new BlockIdentity($data['identity']),
new Owner($data['owner']),
);
} catch (Throwable) {
throw UnserializeFailedException::createFromInput($content);
}

return new Block(
new BlockIdentity($data['identity']),
new Owner($data['owner']),
);
return $block;
}

private function createKey(BlockIdentity $identity): string
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Brainbits\Blocking\Tests\Functional;

use Brainbits\Blocking\Blocker;
use Brainbits\Blocking\Exception\BlockFailedException;
use Brainbits\Blocking\Exception\AlreadyBlockedException;
use Brainbits\Blocking\Identity\BlockIdentity;
use Brainbits\Blocking\Owner\ValueOwnerFactory;
use Brainbits\Blocking\Storage\FilesystemStorage;
Expand Down Expand Up @@ -61,7 +61,7 @@ public function testSelfBlock(): void

public function testOtherBlock(): void
{
$this->expectException(BlockFailedException::class);
$this->expectException(AlreadyBlockedException::class);
$this->expectExceptionMessage('Identifier my_item is already blocked.');

file_put_contents(
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/InMemoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Brainbits\Blocking\Block;
use Brainbits\Blocking\Blocker;
use Brainbits\Blocking\Exception\BlockFailedException;
use Brainbits\Blocking\Exception\AlreadyBlockedException;
use Brainbits\Blocking\Identity\BlockIdentity;
use Brainbits\Blocking\Owner\Owner;
use Brainbits\Blocking\Owner\ValueOwnerFactory;
Expand Down Expand Up @@ -56,7 +56,7 @@ public function testSelfBlock(): void

public function testOtherBlock(): void
{
$this->expectException(BlockFailedException::class);
$this->expectException(AlreadyBlockedException::class);
$this->expectExceptionMessage('Identifier my_item is already blocked.');

$storage = new InMemoryStorage($this->clock);
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/PredisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Brainbits\Blocking\Tests\Functional;

use Brainbits\Blocking\Blocker;
use Brainbits\Blocking\Exception\BlockFailedException;
use Brainbits\Blocking\Exception\AlreadyBlockedException;
use Brainbits\Blocking\Identity\BlockIdentity;
use Brainbits\Blocking\Owner\ValueOwnerFactory;
use Brainbits\Blocking\Storage\PredisStorage;
Expand Down Expand Up @@ -67,7 +67,7 @@ public function testSelfBlock(): void

public function testOtherBlock(): void
{
$this->expectException(BlockFailedException::class);
$this->expectException(AlreadyBlockedException::class);
$this->expectExceptionMessage('Identifier my_item is already blocked.');

$this->client->set('block:my_item', json_encode(['identity' => 'my_item', 'owner' => 'other_owner']));
Expand Down
Loading