Skip to content

Commit d72306c

Browse files
author
Stephan Wentz
committed
fixup! fix: Add redis storage support, add bundle and deprecate brainbits/blocking-bundle, require psr/clock, rework expiration to ttl mechanism implemented by storage
1 parent 2e61292 commit d72306c

14 files changed

Lines changed: 3 additions & 453 deletions

src/Blocker.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
use Brainbits\Blocking\Identity\BlockIdentity;
1818
use Brainbits\Blocking\Owner\OwnerFactoryInterface;
1919
use Brainbits\Blocking\Storage\StorageInterface;
20-
use Brainbits\Blocking\Validator\ValidatorInterface;
2120

2221
final readonly class Blocker
2322
{
2423
public function __construct(
2524
private StorageInterface $storage,
2625
private OwnerFactoryInterface $ownerFactory,
27-
private ValidatorInterface|null $validator = null,
2826
private int $defaultTtl = 60,
2927
) {
3028
}
@@ -79,23 +77,7 @@ public function isBlocked(BlockIdentity $identifier): bool
7977
{
8078
$block = $this->storage->get($identifier);
8179

82-
if (!$block) {
83-
return false;
84-
}
85-
86-
if (!$this->validator) {
87-
return true;
88-
}
89-
90-
$valid = $this->validator->validate($block);
91-
92-
if ($valid) {
93-
return true;
94-
}
95-
96-
$this->storage->remove($block);
97-
98-
return false;
80+
return $block !== null;
9981
}
10082

10183
public function getBlock(BlockIdentity $identifier): Block|null

src/Bundle/DependencyInjection/BrainbitsBlockingExtension.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public function load(array $configs, ContainerBuilder $container): void
4343
$container->setAlias('brainbits_blocking.predis', $config['predis']);
4444
}
4545

46+
$container->setParameter('brainbits_blocking.interval', $config['block_interval']);
47+
4648
if (isset($config['storage']['storage_dir'])) {
4749
$container->setParameter(
4850
'brainbits_blocking.storage.storage_dir',
@@ -64,20 +66,6 @@ public function load(array $configs, ContainerBuilder $container): void
6466
);
6567
}
6668

67-
if (isset($config['validator']['expiration_time'])) {
68-
$container->setParameter(
69-
'brainbits_blocking.validator.expiration_time',
70-
$config['validator']['expiration_time'],
71-
);
72-
$container->setParameter('brainbits_blocking.interval', $config['block_interval']);
73-
}
74-
75-
if ($config['validator']['driver'] !== 'custom') {
76-
$xmlLoader->load(sprintf('validator/%s.xml', $config['validator']['driver']));
77-
} else {
78-
$container->setAlias('brainbits_blocking.validator', $config['validator']['service']);
79-
}
80-
8169
if ($config['storage']['driver'] !== 'custom') {
8270
$xmlLoader->load(sprintf('storage/%s.xml', $config['storage']['driver']));
8371
} else {

src/Bundle/DependencyInjection/Configuration.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public function getConfigTreeBuilder(): TreeBuilder
3030

3131
$storageDrivers = ['filesystem', 'predis', 'in_memory', 'custom'];
3232
$ownerFactoryDrivers = ['symfony_session', 'symfony_token', 'value', 'custom'];
33-
$validatorDrivers = ['expired', 'always_invalidate', 'custom'];
3433

3534
$rootNode
3635
->beforeNormalization()
@@ -86,24 +85,6 @@ public function getConfigTreeBuilder(): TreeBuilder
8685
->scalarNode('value')->end()
8786
->end()
8887
->end()
89-
->arrayNode('validator')
90-
->addDefaultsIfNotSet()
91-
->children()
92-
->scalarNode('driver')
93-
->validate()
94-
->ifNotInArray($validatorDrivers)
95-
->thenInvalid(
96-
'The validator driver %s is not supported. Please choose one of ' .
97-
json_encode($validatorDrivers),
98-
)
99-
->end()
100-
->defaultValue('expired')
101-
->cannotBeEmpty()
102-
->end()
103-
->scalarNode('service')->end()
104-
->integerNode('expiration_time')->defaultValue(300)->end()
105-
->end()
106-
->end()
10788
->end()
10889
->validate()
10990
->ifTrue(static function ($v) {
@@ -118,14 +99,6 @@ public function getConfigTreeBuilder(): TreeBuilder
11899
->thenInvalid(
119100
'You need to specify your own owner_factory service when using the "custom" owner_factory driver.',
120101
)
121-
->end()
122-
->validate()
123-
->ifTrue(static function ($v) {
124-
return $v['validator']['driver'] === 'custom' && empty($v['validator']['service']);
125-
})
126-
->thenInvalid(
127-
'You need to specify your own validator service when using the "custom" validator driver.',
128-
)
129102
->end();
130103

131104
return $treeBuilder;

src/Bundle/Resources/config/services.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ services:
99
arguments:
1010
- '@brainbits_blocking.storage'
1111
- '@brainbits_blocking.owner_factory'
12-
- '@brainbits_blocking.validator'
1312

1413

1514
brainbits_blocking.controller:

src/Bundle/Resources/config/validator/always_invalidate.xml

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Bundle/Resources/config/validator/expired.xml

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Validator/AlwaysInvalidateValidator.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Validator/AlwaysValidateValidator.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Validator/ValidatorInterface.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

tests/BlockerTest.php

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515

1616
use Brainbits\Blocking\Block;
1717
use Brainbits\Blocking\Blocker;
18-
use Brainbits\Blocking\Exception\BlockFailedException;
1918
use Brainbits\Blocking\Identity\BlockIdentity;
2019
use Brainbits\Blocking\Owner\Owner;
2120
use Brainbits\Blocking\Owner\OwnerFactoryInterface;
2221
use Brainbits\Blocking\Owner\ValueOwnerFactory;
2322
use Brainbits\Blocking\Storage\StorageInterface;
24-
use Brainbits\Blocking\Validator\AlwaysInvalidateValidator;
25-
use Brainbits\Blocking\Validator\AlwaysValidateValidator;
2623
use PHPUnit\Framework\MockObject\MockObject;
2724
use PHPUnit\Framework\TestCase;
2825

@@ -62,90 +59,6 @@ public function testBlockReturnsBlockOnNonexistingBlock(): void
6259
$this->assertInstanceOf(Block::class, $result);
6360
}
6461

65-
public function testBlockReturnsBlockOnExistingAndInvalidBlock(): void
66-
{
67-
$storage = $this->createExistingStorage();
68-
$storage->expects($this->once())
69-
->method('remove');
70-
$storage->expects($this->once())
71-
->method('write');
72-
73-
$blocker = new Blocker(
74-
$storage,
75-
$this->ownerFactory,
76-
new AlwaysInvalidateValidator(),
77-
);
78-
$result = $blocker->block($this->identifier);
79-
80-
$this->assertInstanceOf(Block::class, $result);
81-
}
82-
83-
public function testBlockThrowsExceptionOnExistingAndValidAndNonOwnerBlock(): void
84-
{
85-
$this->expectException(BlockFailedException::class);
86-
87-
$storage = $this->createExistingStorage();
88-
$storage->expects($this->never())
89-
->method('write');
90-
91-
$blocker = new Blocker(
92-
$storage,
93-
$this->ownerFactory,
94-
new AlwaysValidateValidator(),
95-
);
96-
$result = $blocker->block($this->identifier);
97-
98-
$this->assertNull($result);
99-
}
100-
101-
public function testBlockUpdatesBlockOnExistingAndValidAndOwnedBlock(): void
102-
{
103-
$storage = $this->createExistingStorage();
104-
$storage->expects($this->once())
105-
->method('touch')
106-
->with($this->block);
107-
108-
$blocker = new Blocker(
109-
$storage,
110-
new ValueOwnerFactory('baz'),
111-
new AlwaysValidateValidator(),
112-
);
113-
$result = $blocker->block($this->identifier);
114-
115-
$this->assertInstanceOf(Block::class, $result);
116-
}
117-
118-
public function testUnblockReturnsNullOnExistingAndInvalidBlock(): void
119-
{
120-
$storage = $this->createExistingStorage();
121-
$storage->expects($this->once())
122-
->method('remove');
123-
124-
$blocker = new Blocker(
125-
$storage,
126-
$this->ownerFactory,
127-
new AlwaysInvalidateValidator(),
128-
);
129-
$result = $blocker->unblock($this->identifier);
130-
131-
$this->assertNull($result);
132-
}
133-
134-
public function testUnblockReturnsBlockOnExistingAndValidBlock(): void
135-
{
136-
$storage = $this->createExistingStorage();
137-
$storage->expects($this->once())
138-
->method('remove');
139-
140-
$blocker = new Blocker(
141-
$storage,
142-
$this->ownerFactory,
143-
);
144-
$result = $blocker->unblock($this->identifier);
145-
146-
$this->assertSame($this->block, $result);
147-
}
148-
14962
public function testUnblockReturnsNullOnNonexistingBlock(): void
15063
{
15164
$storage = $this->createNonexistingStorage();
@@ -161,33 +74,6 @@ public function testUnblockReturnsNullOnNonexistingBlock(): void
16174
$this->assertNull($result);
16275
}
16376

164-
public function testIsBlockedReturnsFalseOnExistingAndInvalidBlock(): void
165-
{
166-
$storage = $this->createExistingStorage();
167-
$storage->expects($this->once())
168-
->method('remove');
169-
170-
$blocker = new Blocker(
171-
$storage,
172-
$this->ownerFactory,
173-
new AlwaysInvalidateValidator(),
174-
);
175-
$result = $blocker->isBlocked($this->identifier);
176-
177-
$this->assertFalse($result);
178-
}
179-
180-
public function testIsBlockedReturnsTrueOnExistingAndValidBlock(): void
181-
{
182-
$blocker = new Blocker(
183-
$this->createExistingStorage(),
184-
$this->ownerFactory,
185-
);
186-
$result = $blocker->isBlocked($this->identifier);
187-
188-
$this->assertTrue($result);
189-
}
190-
19177
public function testIsBlockedReturnsFalseOnNonexistingBlock(): void
19278
{
19379
$blocker = new Blocker(

0 commit comments

Comments
 (0)