Skip to content

Commit 8e573f8

Browse files
committed
added exceptions, added an always invalidating validator
1 parent a9335df commit 8e573f8

26 files changed

Lines changed: 265 additions & 31 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"require": {
1414
"php": ">=5.3.0",
1515
"mikey179/vfsStream": "1.1.*",
16-
"symfony/http-foundation": "2.1.*"
16+
"symfony/http-foundation": ">=2.1"
1717
},
1818
"autoload": {
1919
"psr-0": { "Brainbits": "src/" }

src/Brainbits/Blocking/Adapter/AdapterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* This file is part of the brainbits block package.
3+
* This file is part of the brainbits blocking package.
44
*
55
* @copyright 2012-2013 brainbits GmbH (http://www.brainbits.net)
66
* @license http://www.brainbits.net/LICENCE Dummy Licence

src/Brainbits/Blocking/Adapter/FilesystemAdapter.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* This file is part of the brainbits block package.
3+
* This file is part of the brainbits blocking package.
44
*
55
* @copyright 2012-2013 brainbits GmbH (http://www.brainbits.net)
66
* @license http://www.brainbits.net/LICENCE Dummy Licence
@@ -10,7 +10,8 @@
1010

1111
use Brainbits\Blocking\Identifier\IdentifierInterface;
1212
use Brainbits\Blocking\BlockInterface;
13-
use Brainbits\Blocking\Block;
13+
use Brainbits\Blocking\Exception\DirectoryNotWritableException;
14+
use Brainbits\Blocking\Exception\FileNotWritableException;
1415

1516
/**
1617
* Filesystem block adapter
@@ -110,22 +111,36 @@ public function get(IdentifierInterface $identifier)
110111
*/
111112
protected function getFilename(IdentifierInterface $identifier)
112113
{
113-
return $this->ensureDirectoryExists($this->root) . '/' . $identifier;
114+
return $this->ensureFileIsWritable($this->ensureDirectoryExists($this->root) . '/' . $identifier);
115+
}
116+
117+
/**
118+
* Ensure file is writable
119+
*
120+
* @param string $file
121+
* @return string
122+
* @throws FileNotWritableException
123+
*/
124+
protected function ensureFileIsWritable($file)
125+
{
126+
if (!is_writable(dirname($file))) {
127+
throw new FileNotWritableException('File ' . $file . ' not writable.');
128+
}
129+
130+
return $file;
114131
}
115132

116133
/**
117134
* Ensure directory exists
118135
*
119-
* @param string $filename
136+
* @param string $dirname
120137
* @return string
121-
* @throws \Exception
138+
* @throws DirectoryNotWritableException
122139
*/
123140
protected function ensureDirectoryExists($dirname)
124141
{
125-
if (!file_exists($dirname)) {
126-
if (!mkdir($dirname, 0777, true)) {
127-
throw new \Exception('Can\'t create block dir ' . $dirname);
128-
}
142+
if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
143+
throw new DirectoryNotWritableException('Can\'t create block dir ' . $dirname . '.');
129144
}
130145

131146
return $dirname;

src/Brainbits/Blocking/Block.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* This file is part of the brainbits block package.
3+
* This file is part of the brainbits blocking package.
44
*
55
* @copyright 2012-2013 brainbits GmbH (http://www.brainbits.net)
66
* @license http://www.brainbits.net/LICENCE Dummy Licence

src/Brainbits/Blocking/BlockInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* This file is part of the brainbits block package.
3+
* This file is part of the brainbits blocking package.
44
*
55
* @copyright 2012-2013 brainbits GmbH (http://www.brainbits.net)
66
* @license http://www.brainbits.net/LICENCE Dummy Licence

src/Brainbits/Blocking/BlockableInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* This file is part of the brainbits block package.
3+
* This file is part of the brainbits blocking package.
44
*
55
* @copyright 2012-2013 brainbits GmbH (http://www.brainbits.net)
66
* @license http://www.brainbits.net/LICENCE Dummy Licence

src/Brainbits/Blocking/Blocker.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* This file is part of the brainbits block package.
3+
* This file is part of the brainbits blocking package.
44
*
55
* @copyright 2012-2013 brainbits GmbH (http://www.brainbits.net)
66
* @license http://www.brainbits.net/LICENCE Dummy Licence
@@ -9,9 +9,10 @@
99
namespace Brainbits\Blocking;
1010

1111
use Brainbits\Blocking\Adapter\AdapterInterface;
12-
use Brainbits\Blocking\Validator\ValidatorInterface;
12+
use Brainbits\Blocking\Exception\BlockingException;
1313
use Brainbits\Blocking\Identifier\IdentifierInterface;
1414
use Brainbits\Blocking\Owner\OwnerInterface;
15+
use Brainbits\Blocking\Validator\ValidatorInterface;
1516

1617
/**
1718
* Blocker
@@ -52,13 +53,14 @@ public function __construct(AdapterInterface $adapter, OwnerInterface $owner, Va
5253
*
5354
* @param IdentifierInterface $identifier
5455
* @return BlockInterface
56+
* @throws BlockingException
5557
*/
5658
public function block(IdentifierInterface $identifier)
5759
{
5860
if ($this->isBlocked($identifier)) {
5961
$block = $this->getBlock($identifier);
6062
if ((string)$block->getOwner() !== (string)$this->owner) {
61-
throw new \Exception('Already blocked');
63+
throw new BlockingException('Already blocked');
6264
}
6365
$this->adapter->touch($block);
6466
return $block;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* This file is part of the brainbits blocking package.
4+
*
5+
* @copyright 2012-2013 brainbits GmbH (http://www.brainbits.net)
6+
* @license http://www.brainbits.net/LICENCE Dummy Licence
7+
*/
8+
9+
namespace Brainbits\Blocking\Exception;
10+
11+
/**
12+
* Blocking exception
13+
*
14+
* @author Stephan Wentz <sw@brainbits.net>
15+
*/
16+
class BlockingException extends Exception
17+
{
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* This file is part of the brainbits blocking package.
4+
*
5+
* @copyright 2012-2013 brainbits GmbH (http://www.brainbits.net)
6+
* @license http://www.brainbits.net/LICENCE Dummy Licence
7+
*/
8+
9+
namespace Brainbits\Blocking\Exception;
10+
11+
/**
12+
* Directory not writable exception
13+
*
14+
* @author Stephan Wentz <sw@brainbits.net>
15+
*/
16+
class DirectoryNotWritableException extends Exception
17+
{
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* This file is part of the brainbits blocking package.
4+
*
5+
* @copyright 2012-2013 brainbits GmbH (http://www.brainbits.net)
6+
* @license http://www.brainbits.net/LICENCE Dummy Licence
7+
*/
8+
9+
namespace Brainbits\Blocking\Exception;
10+
11+
/**
12+
* Base exception
13+
*
14+
* @author Stephan Wentz <sw@brainbits.net>
15+
*/
16+
class Exception extends \Exception
17+
{
18+
}

0 commit comments

Comments
 (0)