<?php

/*
 * This file is part of MediaVorus.
 *
 * (c) 2012 Romain Neutron <imprec@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace MediaVorus;

use FFMpeg\Exception\ExecutableNotFoundException;
use MediaVorus\Exception\RuntimeException;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

class MediaVorusServiceProvider implements ServiceProviderInterface
{
    /**
     * {@inheritdoc}
     */
    public function register(Container $container)
    {
        $container['mediavorus'] = function ($c) {
            if (! isset($c['exiftool.reader']) || ! isset($c['exiftool.writer'])) {
                throw new RuntimeException('MediaVorus Service Provider requires Exiftool Service Provider');
            }
            $ffprobe = null;
            if (isset($c['ffmpeg.ffprobe'])) {
                try {
                    $ffprobe = $c['ffmpeg.ffprobe'];
                } catch (ExecutableNotFoundException $e) {
                    // optional: log warning or silently continue
                }
            }

            return new MediaVorus($c['exiftool.reader'], $c['exiftool.writer'], $ffprobe);
        };
    }

    /**
     * {@inheritdoc}
     */
    public function boot(Container $container)
    {
        if (!isset($container['exiftool.reader']) || ! isset($container['exiftool.writer'])) {
            throw new RuntimeException('MediaVorus Service Provider requires Exiftool Service Provider');
        }
    }
}
