|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * This file is part of the brainbits block package. |
| 3 | + * This file is part of the brainbits blocking package. |
4 | 4 | * |
5 | 5 | * @copyright 2012-2013 brainbits GmbH (http://www.brainbits.net) |
6 | 6 | * @license http://www.brainbits.net/LICENCE Dummy Licence |
|
10 | 10 |
|
11 | 11 | use Brainbits\Blocking\Identifier\IdentifierInterface; |
12 | 12 | use Brainbits\Blocking\BlockInterface; |
13 | | -use Brainbits\Blocking\Block; |
| 13 | +use Brainbits\Blocking\Exception\DirectoryNotWritableException; |
| 14 | +use Brainbits\Blocking\Exception\FileNotWritableException; |
14 | 15 |
|
15 | 16 | /** |
16 | 17 | * Filesystem block adapter |
@@ -110,22 +111,36 @@ public function get(IdentifierInterface $identifier) |
110 | 111 | */ |
111 | 112 | protected function getFilename(IdentifierInterface $identifier) |
112 | 113 | { |
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; |
114 | 131 | } |
115 | 132 |
|
116 | 133 | /** |
117 | 134 | * Ensure directory exists |
118 | 135 | * |
119 | | - * @param string $filename |
| 136 | + * @param string $dirname |
120 | 137 | * @return string |
121 | | - * @throws \Exception |
| 138 | + * @throws DirectoryNotWritableException |
122 | 139 | */ |
123 | 140 | protected function ensureDirectoryExists($dirname) |
124 | 141 | { |
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 . '.'); |
129 | 144 | } |
130 | 145 |
|
131 | 146 | return $dirname; |
|
0 commit comments