Skip to content

Commit

Permalink
Merge pull request brainbits#20 from temp/feature/predis-and-cleanup
Browse files Browse the repository at this point in the history
Feature/predis and cleanup
  • Loading branch information
jojoguru authored Jul 21, 2023
2 parents 9912e9a + e39059a commit 615db67
Show file tree
Hide file tree
Showing 63 changed files with 1,821 additions and 958 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/test-redis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: "Test Redis"

on:
push:
pull_request:
schedule:
- cron: '0 03 * * 1' # At 03:00 on Monday.

jobs:
tests:
name: "Tests"

runs-on: ${{ matrix.operating-system }}

strategy:
matrix:
dependencies: ["lowest", "highest"]
php-version:
- "8.2"
operating-system: ["ubuntu-latest"]

steps:
- name: "Checkout"
uses: "actions/checkout@v3"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"

- name: "Cache dependencies"
uses: "actions/cache@v3"
with:
path: "~/.composer/cache"
key: "php-${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }}"
restore-keys: "php-${{ matrix.php-version }}-composer-"

- name: "Install lowest dependencies"
if: ${{ matrix.dependencies == 'lowest' }}
run: "composer update --prefer-lowest --prefer-dist --no-interaction --no-progress --no-suggest"

- name: "Install highest dependencies"
if: ${{ matrix.dependencies == 'highest' }}
run: "composer update --prefer-dist --no-interaction --no-progress --no-suggest"

- name: "Start Redis"
uses: "supercharge/[email protected]"
with:
redis-version: "6"

- name: "Unit tests"
run: "REDIS_DSN='redis://127.0.0.1:6379' vendor/bin/phpunit --group redis --fail-on-skipped"
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ jobs:
matrix:
dependencies: ["lowest", "highest"]
php-version:
- "8.1"
- "8.2"
operating-system: ["ubuntu-latest"]

Expand Down Expand Up @@ -46,7 +45,7 @@ jobs:
run: "composer update --prefer-dist --no-interaction --no-progress --no-suggest"

- name: "Unit tests"
run: "vendor/bin/phpunit"
run: "vendor/bin/phpunit --exclude-group redis"

- name: "Coding style"
run: "vendor/bin/phpcs --report=summary"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.idea/
/.phpcs-cache
/.phpunit.cache
/composer.lock
/phpcs.xml
Expand Down
20 changes: 16 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,30 @@
}
],
"require": {
"php": "^8.1"
"php": "^8.2",
"psr/clock": "^1.0"
},
"require-dev": {
"brainbits/phpcs-standard": "^7.0",
"brainbits/phpstan-rules": "^3.0",
"matthiasnoback/symfony-config-test": "^4.3",
"matthiasnoback/symfony-dependency-injection-test": "^4.3",
"mikey179/vfsstream": "^1.6.10",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^10.1",
"predis/predis": "^2.2",
"symfony/clock": "^6.3",
"symfony/config": "^6.0",
"symfony/dependency-injection": "^6.0",
"symfony/http-foundation": "^6.0",
"symfony/http-kernel": "^6.0",
"symfony/routing": "^6.0",
"symfony/security-core": "^6.0",
"brainbits/phpcs-standard": "^7.0",
"phpstan/phpstan": "^1.0",
"brainbits/phpstan-rules": "^3.0"
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-symfony": "^1.3"
},
"suggest": {
"predis/predis": "If you want to use the PredisStorage",
"symfony/http-foundation": "If you want to use the SymfonySessionOwnerFactory",
"symfony/security-core": "If you want to use the SymfonyTokenOwnerFactory"
},
Expand Down
23 changes: 20 additions & 3 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<file extension="php">src/</file>
<arg name="basepath" value="." />
<arg name="colors" />

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

<!-- Start here -->
<arg name="basepath" value="."/>
<!-- Only .php -->
<arg name="extensions" value="php"/>
<!-- 80 parallel -->
<arg name="parallel" value="10"/>
<!-- User cache dir -->
<arg name="cache" value=".phpcs-cache"/>
<!-- Show colors -->
<arg name="colors"/>
<!-- Show progress -->
<arg value="p"/>
<!-- Show sniff names -->
<arg value="s"/>

<rule ref="Brainbits">
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols" />
<exclude name="SlevomatCodingStandard.Classes.SuperfluousExceptionNaming.SuperfluousSuffix" />
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming.SuperfluousSuffix" />
</rule>
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ parameters:
- vendor/autoload.php
includes:
- vendor/brainbits/phpstan-rules/rules.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-symfony/extension.neon
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuite name="blocking">
<directory suffix=".php">tests</directory>
</testsuite>
Expand Down
39 changes: 8 additions & 31 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,29 @@

namespace Brainbits\Blocking;

use Brainbits\Blocking\Identity\IdentityInterface;
use Brainbits\Blocking\Owner\OwnerInterface;
use DateTimeImmutable;
use Brainbits\Blocking\Identity\BlockIdentity;
use Brainbits\Blocking\Owner\Owner;

/**
* Standard block.
*/
class Block implements BlockInterface
final class Block
{
private DateTimeImmutable $updatedAt;

public function __construct(
private IdentityInterface $identifier,
private OwnerInterface $owner,
private DateTimeImmutable $createdAt,
private BlockIdentity $identifier,
private Owner $owner,
) {
$this->updatedAt = $createdAt;
}

public function getIdentity(): IdentityInterface
public function getIdentity(): BlockIdentity
{
return $this->identifier;
}

public function getOwner(): OwnerInterface
public function getOwner(): Owner
{
return $this->owner;
}

public function isOwnedBy(OwnerInterface $owner): bool
public function isOwnedBy(Owner $owner): bool
{
return $this->owner->equals($owner);
}

public function getCreatedAt(): DateTimeImmutable
{
return $this->createdAt;
}

public function getUpdatedAt(): DateTimeImmutable
{
return $this->updatedAt;
}

public function touch(DateTimeImmutable $updatedAt): void
{
$this->updatedAt = $updatedAt;
}
}
36 changes: 0 additions & 36 deletions src/BlockInterface.php

This file was deleted.

27 changes: 0 additions & 27 deletions src/BlockableInterface.php

This file was deleted.

48 changes: 14 additions & 34 deletions src/Blocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,22 @@
namespace Brainbits\Blocking;

use Brainbits\Blocking\Exception\BlockFailedException;
use Brainbits\Blocking\Identity\IdentityInterface;
use Brainbits\Blocking\Identity\BlockIdentity;
use Brainbits\Blocking\Owner\OwnerFactoryInterface;
use Brainbits\Blocking\Storage\StorageInterface;
use Brainbits\Blocking\Validator\ValidatorInterface;
use DateTimeImmutable;

/**
* Blocker.
*/
class Blocker
final readonly class Blocker
{
private StorageInterface $storage;

public function __construct(
StorageInterface $adapter,
private StorageInterface $storage,
private OwnerFactoryInterface $ownerFactory,
private ValidatorInterface $validator,
private int $defaultTtl = 60,
) {
$this->storage = $adapter;
}

public function block(IdentityInterface $identifier): BlockInterface
public function block(BlockIdentity $identifier, int|null $ttl = null): Block
{
$block = $this->tryBlock($identifier);
$block = $this->tryBlock($identifier, $ttl);

if ($block === null) {
throw BlockFailedException::createAlreadyBlocked($identifier);
Expand All @@ -46,7 +38,7 @@ public function block(IdentityInterface $identifier): BlockInterface
return $block;
}

public function tryBlock(IdentityInterface $identifier): BlockInterface|null
public function tryBlock(BlockIdentity $identifier, int|null $ttl = null): Block|null
{
$owner = $this->ownerFactory->createOwner();

Expand All @@ -57,19 +49,19 @@ public function tryBlock(IdentityInterface $identifier): BlockInterface|null
return null;
}

$this->storage->touch($block);
$this->storage->touch($block, $ttl ?? $this->defaultTtl);

return $block;
}

$block = new Block($identifier, $owner, new DateTimeImmutable());
$block = new Block($identifier, $owner);

$this->storage->write($block);
$this->storage->write($block, $ttl ?? $this->defaultTtl);

return $block;
}

public function unblock(IdentityInterface $identifier): BlockInterface|null
public function unblock(BlockIdentity $identifier): Block|null
{
$block = $this->getBlock($identifier);
if ($block === null) {
Expand All @@ -81,26 +73,14 @@ public function unblock(IdentityInterface $identifier): BlockInterface|null
return $block;
}

public function isBlocked(IdentityInterface $identifier): bool
public function isBlocked(BlockIdentity $identifier): bool
{
$block = $this->storage->get($identifier);

if (!$block) {
return false;
}

$valid = $this->validator->validate($block);

if ($valid) {
return true;
}

$this->storage->remove($block);

return false;
return $block !== null;
}

public function getBlock(IdentityInterface $identifier): BlockInterface|null
public function getBlock(BlockIdentity $identifier): Block|null
{
if (!$this->isBlocked($identifier)) {
return null;
Expand Down
Loading

0 comments on commit 615db67

Please sign in to comment.