diff --git a/Adapter/Gaufrette/AbstractCmfMediaDoctrine.php b/Adapter/Gaufrette/AbstractCmfMediaDoctrine.php index 4ef76e6..4341481 100644 --- a/Adapter/Gaufrette/AbstractCmfMediaDoctrine.php +++ b/Adapter/Gaufrette/AbstractCmfMediaDoctrine.php @@ -13,6 +13,7 @@ use Symfony\Cmf\Bundle\MediaBundle\HierarchyInterface; use Symfony\Cmf\Bundle\MediaBundle\MediaInterface; use Symfony\Cmf\Bundle\MediaBundle\MediaManagerInterface; +use Symfony\Cmf\Bundle\MediaBundle\MetadataInterface; /** * Cmf doctrine media adapter @@ -133,6 +134,9 @@ public function write($key, $content) { if ($this->exists($key)) { $file = $this->find($key); + if (! $file instanceof FileInterface) { + return false; + } } else { $filePath = $this->computePath($key); @@ -289,8 +293,8 @@ public function listKeys($prefix = '') /** * {@inheritDoc} * - * @throws RuntimeException If file cannot be found or cannot be written - * to + * @throws \RuntimeException If file cannot be found or cannot be written + * to */ public function setMetadata($key, $metadata) { @@ -300,6 +304,14 @@ public function setMetadata($key, $metadata) throw new \RuntimeException(sprintf('The file "%s" does not exist.', $key)); } + if (! $file instanceof MetadataInterface) { + $type = is_object($file) ? get_class($file) : gettype($file); + throw new \InvalidArgumentException(sprintf( + 'The class "%s" does not implement Symfony\Cmf\Bundle\MediaBundle\MetadataInterface', + $type + )); + } + $file->setMetadata($metadata); } @@ -351,6 +363,7 @@ public function setAutoFlush($bool) * * @param string|int $key Identifier. * @param boolean $dir directly try to find a directory + * * @return FileInterface */ protected function find($key, $dir = false) diff --git a/Form/DataTransformer/ModelToFileTransformer.php b/Form/DataTransformer/ModelToFileTransformer.php index 85d28c8..6003a49 100644 --- a/Form/DataTransformer/ModelToFileTransformer.php +++ b/Form/DataTransformer/ModelToFileTransformer.php @@ -14,6 +14,13 @@ class ModelToFileTransformer implements DataTransformerInterface public function __construct($class) { + if (!is_subclass_of($class, 'Symfony\Cmf\Bundle\MediaBundle\FileInterface')) { + throw new \InvalidArgumentException(sprintf( + 'The class "%s" does not implement Symfony\Cmf\Bundle\MediaBundle\FileInterface', + $class + )); + } + $this->dataClass = $class; } diff --git a/MetadataInterface.php b/MetadataInterface.php index 7cdc159..9469382 100644 --- a/MetadataInterface.php +++ b/MetadataInterface.php @@ -20,7 +20,6 @@ public function getDescription(); /** * @param string $description - * @return void */ public function setDescription($description); @@ -33,7 +32,6 @@ public function getCopyright(); /** * @param string $copyright - * @return void */ public function setCopyright($copyright); @@ -46,7 +44,6 @@ public function getAuthorName(); /** * @param string $author - * @return void */ public function setAuthorName($author); @@ -66,7 +63,9 @@ public function setMetadata(array $metadata); /** * @param string $name - * @param null $default to be used if $name is not set in the metadata + * @param string $default to be used if $name is not set in the metadata + * + * @return string */ public function getMetadataValue($name, $default = null); @@ -74,7 +73,7 @@ public function getMetadataValue($name, $default = null); * The metadata value * * @param string $name - * @param mixed $value + * @param string $value */ public function setMetadataValue($name, $value); diff --git a/Model/Media.php b/Model/Media.php index 28dfe2e..8db6cb4 100644 --- a/Model/Media.php +++ b/Model/Media.php @@ -27,7 +27,7 @@ class Media extends BaseMedia implements MetadataInterface protected $metadata; /** - * @param string $description + * {@inheritDoc} */ public function setDescription($description) { @@ -35,7 +35,7 @@ public function setDescription($description) } /** - * @return string + * {@inheritDoc} */ public function getDescription() { @@ -43,7 +43,7 @@ public function getDescription() } /** - * @param string $copyright + * {@inheritDoc} */ public function setCopyright($copyright) { @@ -51,7 +51,7 @@ public function setCopyright($copyright) } /** - * @return string + * {@inheritDoc} */ public function getCopyright() { @@ -59,7 +59,7 @@ public function getCopyright() } /** - * @param string $authorName + * {@inheritDoc} */ public function setAuthorName($authorName) { @@ -67,7 +67,7 @@ public function setAuthorName($authorName) } /** - * @return string + * {@inheritDoc} */ public function getAuthorName() { @@ -75,7 +75,7 @@ public function getAuthorName() } /** - * @param array $metadata + * {@inheritDoc} */ public function setMetadata(array $metadata) { @@ -83,7 +83,7 @@ public function setMetadata(array $metadata) } /** - * @return array + * {@inheritDoc} */ public function getMetadata() { @@ -91,18 +91,15 @@ public function getMetadata() } /** - * @param string $name - * @param null $default - * @return null + * {@inheritDoc} */ public function getMetadataValue($name, $default = null) { - return isset($metadata[$name]) ? $metadata[$name] : $default; + return isset($this->metadata[$name]) ? $this->metadata[$name] : $default; } /** - * @param string $name - * @param mixed $value + * {@inheritDoc} */ public function setMetadataValue($name, $value) { @@ -114,8 +111,6 @@ public function setMetadataValue($name, $value) */ public function unsetMetadataValue($name) { - $metadata = $this->getMetadata(); - unset($metadata[$name]); - $this->setMetadata($metadata); + unset($this->metadata[$name]); } }