Skip to content

Commit

Permalink
Merge pull request #604 from KnpLabs/feature/filesystem-map-interface
Browse files Browse the repository at this point in the history
declare a FilesystemMapInterface
  • Loading branch information
nicolasmure committed Jun 4, 2019
2 parents 80cc42e + c86cbac commit 33b0216
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 39 deletions.
2 changes: 1 addition & 1 deletion spec/Gaufrette/FilesystemMapSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function it_sets_mapped_filesystem(Filesystem $filesystem)
function it_fails_when_get_filesystem_which_was_not_mapped()
{
$this
->shouldThrow(new \InvalidArgumentException('There is no filesystem defined for the "some" domain.'))
->shouldThrow(new \InvalidArgumentException('There is no filesystem defined having "some" name.'))
->duringGet('some')
;
}
Expand Down
64 changes: 26 additions & 38 deletions src/Gaufrette/FilesystemMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
namespace Gaufrette;

/**
* Associates filesystem instances to domains.
* Associates filesystem instances to their names.
*
* @author Antoine Hérault <antoine.herault@gmail.com>
*/
class FilesystemMap
class FilesystemMap implements FilesystemMapInterface
{
private $filesystems = array();

/**
* Returns an array of all the registered filesystems where the key is the
* domain and the value the filesystem.
* name and the value the filesystem.
*
* @return array
*/
Expand All @@ -23,76 +23,64 @@ public function all()
}

/**
* Register the given filesystem for the specified domain.
* Register the given filesystem for the specified name.
*
* @param string $domain
* @param string $name
* @param FilesystemInterface $filesystem
*
* @throws \InvalidArgumentException when the specified domain contains
* @throws \InvalidArgumentException when the specified name contains
* forbidden characters
*/
public function set($domain, FilesystemInterface $filesystem)
public function set($name, FilesystemInterface $filesystem)
{
if (!preg_match('/^[-_a-zA-Z0-9]+$/', $domain)) {
if (!preg_match('/^[-_a-zA-Z0-9]+$/', $name)) {
throw new \InvalidArgumentException(sprintf(
'The specified domain "%s" is not a valid domain.',
$domain
'The specified name "%s" is not valid.',
$name
));
}

$this->filesystems[$domain] = $filesystem;
$this->filesystems[$name] = $filesystem;
}

/**
* Indicates whether there is a filesystem registered for the specified
* domain.
*
* @param string $domain
*
* @return bool
* {@inheritdoc}
*/
public function has($domain)
public function has($name)
{
return isset($this->filesystems[$domain]);
return isset($this->filesystems[$name]);
}

/**
* Returns the filesystem registered for the specified domain.
*
* @param string $domain
*
* @return FilesystemInterface
*
* @throw \InvalidArgumentException when there is no filesystem registered
* for the specified domain
* {@inheritdoc}
*/
public function get($domain)
public function get($name)
{
if (!$this->has($domain)) {
if (!$this->has($name)) {
throw new \InvalidArgumentException(sprintf(
'There is no filesystem defined for the "%s" domain.',
$domain
'There is no filesystem defined having "%s" name.',
$name
));
}

return $this->filesystems[$domain];
return $this->filesystems[$name];
}

/**
* Removes the filesystem registered for the specified domain.
* Removes the filesystem registered for the specified name.
*
* @param string $domain
* @param string $name
*/
public function remove($domain)
public function remove($name)
{
if (!$this->has($domain)) {
if (!$this->has($name)) {
throw new \InvalidArgumentException(sprintf(
'Cannot remove the "%s" filesystem as it is not defined.',
$domain
$name
));
}

unset($this->filesystems[$domain]);
unset($this->filesystems[$name]);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/Gaufrette/FilesystemMapInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Gaufrette;

/**
* Associates filesystem instances to their names.
*/
interface FilesystemMapInterface
{
/**
* Indicates whether there is a filesystem registered for the specified
* name.
*
* @param string $name
*
* @return bool
*/
public function has($name);

/**
* Returns the filesystem registered for the specified name.
*
* @param string $name
*
* @return FilesystemInterface
*
* @throw \InvalidArgumentException when there is no filesystem registered
* for the specified name
*/
public function get($name);
}

0 comments on commit 33b0216

Please sign in to comment.