1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 
<?php
namespace Omeka\Media\Renderer;

use Omeka\Api\Representation\MediaRepresentation;
use Omeka\Media\FileRenderer\Manager as FileRendererManager;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\View\Renderer\PhpRenderer;

class File implements RendererInterface
{
    /**
     * @var FileRendererManager
     */
    protected $fileRendererManager;

    /**
     * @param FileRendererManager $fileRendererManager
     */
    public function __construct(FileRendererManager $fileRendererManager)
    {
        $this->fileRendererManager = $fileRendererManager;
    }

    /**
     * {@inheritDoc}
     */
    public function render(PhpRenderer $view, MediaRepresentation $media,
        array $options = []
    ) {
        try {
            $renderer = $this->fileRendererManager->get($media->mediaType());
        } catch (ServiceNotFoundException $e) {
            try {
                $renderer = $this->fileRendererManager->get($media->extension());
            } catch (ServiceNotFoundException $e) {
                if ($media->hasThumbnails()) {
                    $renderer = $this->fileRendererManager->get('thumbnail');
                } else {
                    $renderer = $this->fileRendererManager->get('fallback');
                }
            }
        }
        return $renderer->render($view, $media, $options);
    }
}