Skip to content

Commit

Permalink
Add priority to media handlers
Browse files Browse the repository at this point in the history
Update tests

Update tests

Update tests
  • Loading branch information
arneruy committed May 5, 2015
1 parent c224514 commit 0bb5b96
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/Kunstmaan/MediaBundle/Helper/File/FileHandler.php
Expand Up @@ -48,8 +48,9 @@ class FileHandler extends AbstractMediaHandler
/**
* Constructor
*/
public function __construct(MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory)
public function __construct($priority, MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory)
{
parent::__construct($priority);
$this->mimeTypeGuesser = $mimeTypeGuesserFactory->get();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Kunstmaan/MediaBundle/Helper/Image/ImageHandler.php
Expand Up @@ -18,9 +18,9 @@ class ImageHandler extends FileHandler
/**
* @param string $aviaryApiKey The aviary key
*/
public function __construct(MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory, $aviaryApiKey)
public function __construct($priority, MimeTypeGuesserFactoryInterface $mimeTypeGuesserFactory, $aviaryApiKey)
{
parent::__construct($mimeTypeGuesserFactory);
parent::__construct($priority, $mimeTypeGuesserFactory);
$this->aviaryApiKey = $aviaryApiKey;
}

Expand Down
18 changes: 18 additions & 0 deletions src/Kunstmaan/MediaBundle/Helper/Media/AbstractMediaHandler.php
Expand Up @@ -11,6 +11,24 @@
*/
abstract class AbstractMediaHandler
{
private $priority;

/**
* @param int $priority
*/
function __construct($priority = 0)
{
$this->priority = $priority;
}

/**
* @return int
*/
public function getPriority()
{
return $this->priority;
}

/**
* @return string
*/
Expand Down
18 changes: 10 additions & 8 deletions src/Kunstmaan/MediaBundle/Helper/MediaManager.php
Expand Up @@ -45,39 +45,41 @@ public function setDefaultHandler(AbstractMediaHandler $handler)
}

/**
* Returns handler to handle the Media item which can handle the item. If no handler is found, it returns FileHandler
* Returns handler with the highest priority to handle the Media item which can handle the item. If no handler is found, it returns FileHandler
*
* @param Media $media
*
* @return AbstractMediaHandler
*/
public function getHandler($media)
{
$bestHandler = $this->defaultHandler;
foreach ($this->handlers as $handler) {
if ($handler->canHandle($media)) {
return $handler;
if ($handler->canHandle($media) && $handler->getPriority() > $bestHandler->getPriority()) {
$bestHandler = $handler;
}
}

return $this->defaultHandler;
return $bestHandler;
}

/**
* Returns handler to handle the Media item based on the Type. If no handler is found, it returns FileHandler
* Returns handler with the highest priority to handle the Media item based on the Type. If no handler is found, it returns FileHandler
*
* @param string $type
*
* @return AbstractMediaHandler
*/
public function getHandlerForType($type)
{
$bestHandler = $this->defaultHandler;
foreach ($this->handlers as $handler) {
if ($handler->getType() == $type) {
return $handler;
if ($handler->getType() == $type && $handler->getPriority() > $bestHandler->getPriority()) {
$bestHandler = $handler;
}
}

return $this->defaultHandler;
return $bestHandler;
}

/**
Expand Down
Expand Up @@ -27,8 +27,9 @@ class RemoteAudioHandler extends AbstractMediaHandler
*/
const TYPE = 'audio';

public function __construct($soundcloudApiKey)
public function __construct($priority, $soundcloudApiKey)
{
parent::__construct($priority);
$this->soundcloudApiKey = $soundcloudApiKey;
}

Expand Down
Expand Up @@ -29,8 +29,9 @@ class RemoteVideoHandler extends AbstractMediaHandler
* Constructor. Takes the configuration of the RemoveVideoHandler
* @param array $configuration
*/
public function __construct($configuration = array())
public function __construct($priority, $configuration = array())
{
parent::__construct($priority);
$this->configuration = $configuration;
}

Expand Down
9 changes: 5 additions & 4 deletions src/Kunstmaan/MediaBundle/Resources/config/handlers.yml
Expand Up @@ -9,24 +9,25 @@ parameters:
services:
kunstmaan_media.media_handlers.remote_slide:
class: "%kunstmaan_media.media_handler.remote_slide.class%"
arguments: [1]
tags:
- { name: 'kunstmaan_media.media_handler' }

kunstmaan_media.media_handlers.remote_video:
class: "%kunstmaan_media.media_handler.remote_video.class%"
arguments: ["%kunstmaan_media.remote_video%"]
arguments: [1, "%kunstmaan_media.remote_video%"]
tags:
- { name: 'kunstmaan_media.media_handler' }

kunstmaan_media.media_handlers.remote_audio:
class: "%kunstmaan_media.media_handler.remote_audio.class%"
arguments: ["%kunstmaan_media.soundcloud_api_key%"]
arguments: [1, "%kunstmaan_media.soundcloud_api_key%"]
tags:
- { name: 'kunstmaan_media.media_handler' }

kunstmaan_media.media_handlers.image:
class: "%kunstmaan_media.media_handler.image.class%"
arguments: ["@kunstmaan_media.mimetype_guesser.factory", "%aviary_api_key%"]
arguments: [1, "@kunstmaan_media.mimetype_guesser.factory", "%aviary_api_key%"]
calls:
- [ setMediaPath, [ "%kernel.root_dir%" ] ]
- [ setBlacklistedExtensions, [ "%kunstmaan_media.blacklisted_extensions%" ] ]
Expand All @@ -35,7 +36,7 @@ services:

kunstmaan_media.media_handlers.file:
class: "%kunstmaan_media.media_handler.file.class%"
arguments: ["@kunstmaan_media.mimetype_guesser.factory"]
arguments: [0, "@kunstmaan_media.mimetype_guesser.factory"]
calls:
- [ setMediaPath, [ "%kernel.root_dir%" ] ]
- [ setBlacklistedExtensions, [ "%kunstmaan_media.blacklisted_extensions%" ] ]
Expand Down
Expand Up @@ -33,7 +33,7 @@ protected function setUp()
$factory = $this->getMock('Kunstmaan\MediaBundle\Helper\MimeTypeGuesserFactoryInterface');
$this->filesDir = realpath(__DIR__ . '/../../Files');

$this->object = new PdfHandler($factory);
$this->object = new PdfHandler(1, $factory);
$this->object->setPdfTransformer($this->pdfTransformer);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Kunstmaan/MediaBundle/Tests/Helper/MediaManagerTest.php
Expand Up @@ -23,7 +23,7 @@ class MediaManagerTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
$this->defaultHandler = $this->getMockForAbstractClass('Kunstmaan\MediaBundle\Helper\Media\AbstractMediaHandler');
$this->defaultHandler = $this->getMockForAbstractClass('Kunstmaan\MediaBundle\Helper\Media\AbstractMediaHandler', array(0));
$this->defaultHandler
->expects($this->any())
->method('canHandle')
Expand Down Expand Up @@ -262,7 +262,7 @@ public function testGetFolderAddActions()
*/
protected function getCustomHandler($media = null, $name = null)
{
$handler = $this->getMockForAbstractClass('Kunstmaan\MediaBundle\Helper\Media\AbstractMediaHandler');
$handler = $this->getMockForAbstractClass('Kunstmaan\MediaBundle\Helper\Media\AbstractMediaHandler', array(0));
if (empty($name)) {
$name = 'CustomHandler';
}
Expand Down

0 comments on commit 0bb5b96

Please sign in to comment.