Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MediaBundle] Add priority to media handlers #394

Merged
merged 1 commit into from
May 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Kunstmaan/MediaBundle/Helper/File/FileHandler.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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(1));
if (empty($name)) {
$name = 'CustomHandler';
}
Expand Down