Skip to content

Commit 6e1ca90

Browse files
temppl-github
authored andcommitted
refactor owner to owner factories, with a simple owner value object (#7)
* refactor owner to owner factories, with a simple owner value object * remove obsolete toString method
1 parent 00341d5 commit 6e1ca90

31 files changed

Lines changed: 534 additions & 255 deletions

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
"require-dev": {
1717
"mikey179/vfsStream": "^1.2",
1818
"phpunit/phpunit": "^6.0",
19-
"symfony/http-foundation": "^2.8|^3.0"
19+
"symfony/http-foundation": "^2.8|^3.0",
20+
"symfony/security-core": "^2.8|^3.0"
21+
},
22+
"suggest": {
23+
"symfony/http-foundation": "If you want to use the SymfonySessionOwnerFactory",
24+
"symfony/security-core": "If you want to use the SymfonyTokenOwnerFactory"
2025
},
2126
"autoload": {
2227
"psr-4": { "Brainbits\\Blocking\\": "src/" }

src/Block.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Brainbits\Blocking;
1515

16-
use Brainbits\Blocking\Identifier\IdentifierInterface;
16+
use Brainbits\Blocking\Identity\IdentityInterface;
1717
use Brainbits\Blocking\Owner\OwnerInterface;
1818
use DateTimeImmutable;
1919

@@ -27,15 +27,15 @@ class Block implements BlockInterface
2727
private $createdAt;
2828
private $updatedAt;
2929

30-
public function __construct(IdentifierInterface $identifier, OwnerInterface $owner, DateTimeImmutable $createdAt)
30+
public function __construct(IdentityInterface $identifier, OwnerInterface $owner, DateTimeImmutable $createdAt)
3131
{
3232
$this->identifier = $identifier;
3333
$this->owner = $owner;
3434
$this->createdAt = $createdAt;
3535
$this->updatedAt = $createdAt;
3636
}
3737

38-
public function getIdentifier(): IdentifierInterface
38+
public function getIdentity(): IdentityInterface
3939
{
4040
return $this->identifier;
4141
}

src/BlockInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Brainbits\Blocking;
1515

16-
use Brainbits\Blocking\Identifier\IdentifierInterface;
16+
use Brainbits\Blocking\Identity\IdentityInterface;
1717
use Brainbits\Blocking\Owner\OwnerInterface;
1818
use DateTimeImmutable;
1919

@@ -22,7 +22,7 @@
2222
*/
2323
interface BlockInterface
2424
{
25-
public function getIdentifier(): IdentifierInterface;
25+
public function getIdentity(): IdentityInterface;
2626

2727
public function getOwner(): OwnerInterface;
2828

src/BlockableInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Brainbits\Blocking;
1515

16-
use Brainbits\Blocking\Identifier\IdentifierInterface;
16+
use Brainbits\Blocking\Identity\IdentityInterface;
1717

1818
/**
1919
* Blockable interface.
@@ -23,7 +23,7 @@ interface BlockableInterface
2323
/**
2424
* Return block identifier
2525
*
26-
* @return IdentifierInterface
26+
* @return IdentityInterface
2727
*/
2828
public function getBlockIdentifier();
2929
}

src/Blocker.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
namespace Brainbits\Blocking;
1515

16-
use Brainbits\Blocking\Storage\StorageInterface;
1716
use Brainbits\Blocking\Exception\BlockFailedException;
18-
use Brainbits\Blocking\Identifier\IdentifierInterface;
19-
use Brainbits\Blocking\Owner\OwnerInterface;
17+
use Brainbits\Blocking\Identity\IdentityInterface;
18+
use Brainbits\Blocking\Owner\OwnerFactoryInterface;
19+
use Brainbits\Blocking\Storage\StorageInterface;
2020
use Brainbits\Blocking\Validator\ValidatorInterface;
2121
use DateTimeImmutable;
2222

@@ -26,17 +26,17 @@
2626
class Blocker
2727
{
2828
private $storage;
29-
private $owner;
29+
private $ownerFactory;
3030
private $validator;
3131

32-
public function __construct(StorageInterface $adapter, OwnerInterface $owner, ValidatorInterface $validator)
32+
public function __construct(StorageInterface $adapter, OwnerFactoryInterface $ownerFactory, ValidatorInterface $validator)
3333
{
3434
$this->storage = $adapter;
35-
$this->owner = $owner;
35+
$this->ownerFactory = $ownerFactory;
3636
$this->validator = $validator;
3737
}
3838

39-
public function block(IdentifierInterface $identifier): BlockInterface
39+
public function block(IdentityInterface $identifier): BlockInterface
4040
{
4141
$block = $this->tryBlock($identifier);
4242

@@ -47,27 +47,28 @@ public function block(IdentifierInterface $identifier): BlockInterface
4747
return $block;
4848
}
4949

50-
public function tryBlock(IdentifierInterface $identifier): ?BlockInterface
50+
public function tryBlock(IdentityInterface $identifier): ?BlockInterface
5151
{
52+
$owner = $this->ownerFactory->createOwner();
53+
5254
if ($this->isBlocked($identifier)) {
5355
$block = $this->getBlock($identifier);
54-
if (!$block->isOwnedBy($this->owner)) {
56+
if (!$block->isOwnedBy($owner)) {
5557
return null;
5658
}
5759
$this->storage->touch($block);
5860

5961
return $block;
6062
}
6163

62-
$block = new Block($identifier, $this->owner, new DateTimeImmutable());
64+
$block = new Block($identifier, $owner, new DateTimeImmutable());
6365

6466
$this->storage->write($block);
6567

6668
return $block;
6769
}
6870

69-
70-
public function unblock(IdentifierInterface $identifier): ?BlockInterface
71+
public function unblock(IdentityInterface $identifier): ?BlockInterface
7172
{
7273
$block = $this->getBlock($identifier);
7374
if (null === $block) {
@@ -79,7 +80,7 @@ public function unblock(IdentifierInterface $identifier): ?BlockInterface
7980
return $block;
8081
}
8182

82-
public function isBlocked(IdentifierInterface $identifier): bool
83+
public function isBlocked(IdentityInterface $identifier): bool
8384
{
8485
$exists = $this->storage->exists($identifier);
8586

@@ -99,7 +100,7 @@ public function isBlocked(IdentifierInterface $identifier): bool
99100
return false;
100101
}
101102

102-
public function getBlock(IdentifierInterface $identifier): ?BlockInterface
103+
public function getBlock(IdentityInterface $identifier): ?BlockInterface
103104
{
104105
if (!$this->isBlocked($identifier)) {
105106
return null;

src/Exception/BlockFailedException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
namespace Brainbits\Blocking\Exception;
1515

16-
use Brainbits\Blocking\Identifier\IdentifierInterface;
16+
use Brainbits\Blocking\Identity\IdentityInterface;
1717

1818
/**
1919
* Block failed exception.
2020
*/
2121
class BlockFailedException extends RuntimeException
2222
{
23-
public static function createAlreadyBlocked(IdentifierInterface $identifier): self
23+
public static function createAlreadyBlocked(IdentityInterface $identifier): self
2424
{
2525
return new self("Identifier $identifier is already blocked.");
2626
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
/*
6+
* This file is part of the brainbits blocking package.
7+
*
8+
* (c) brainbits GmbH (http://www.brainbits.net)
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Brainbits\Blocking\Exception;
15+
16+
/**
17+
* No token found exception.
18+
*/
19+
class NoTokenFoundException extends RuntimeException
20+
{
21+
public static function create(): self
22+
{
23+
return new self("No token found.");
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
/*
6+
* This file is part of the brainbits blocking package.
7+
*
8+
* (c) brainbits GmbH (http://www.brainbits.net)
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Brainbits\Blocking\Exception;
15+
16+
/**
17+
* No user found exception.
18+
*/
19+
class NoUserFoundException extends RuntimeException
20+
{
21+
public static function create(): self
22+
{
23+
return new self("No user found.");
24+
}
25+
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14-
namespace Brainbits\Blocking\Identifier;
14+
namespace Brainbits\Blocking\Identity;
1515

1616
/**
1717
* Standard identifier.
1818
*/
19-
class Identifier implements IdentifierInterface
19+
class Identity implements IdentityInterface
2020
{
21-
private $identifier;
21+
private $identityValue;
2222

23-
public function __construct(string $identifier)
23+
public function __construct(string $identityValue)
2424
{
25-
$this->identifier = $identifier;
25+
$this->identityValue = $identityValue;
2626
}
2727

28-
public function equals(IdentifierInterface $identifier): bool
28+
public function equals(IdentityInterface $identifier): bool
2929
{
30-
return (string) $identifier === (string) $this->identifier;
30+
return (string) $identifier === (string) $this;
3131
}
3232

3333
public function __toString(): string
3434
{
35-
return $this->identifier;
35+
return $this->identityValue;
3636
}
3737
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
* file that was distributed with this source code.
1212
*/
1313

14-
namespace Brainbits\Blocking\Identifier;
14+
namespace Brainbits\Blocking\Identity;
1515

1616
/**
17-
* Block identifier interface.
17+
* Block identity interface.
1818
*/
19-
interface IdentifierInterface
19+
interface IdentityInterface
2020
{
21-
public function equals(IdentifierInterface $identifier): bool;
21+
public function equals(IdentityInterface $identifier): bool;
2222

2323
public function __toString(): string;
2424
}

0 commit comments

Comments
 (0)