<?php

namespace Lightscale\LaralightSvg\Http\Controllers;

use Lightscale\LaralightSvg\SvgService;

use Illuminate\Routing\Controller;
use Illuminate\Http\Request;

class SvgController extends Controller
{

    public function serveSvg(SvgService $svg, Request $request, string $collection)
    {
        $collection = $svg->getCollection($collection);
        if ($collection === null) abort(404);

        $svgContent = $collection->getSvg();
        if ($svgContent === null) abort(404);

        $cacheControl = '';
        if ($request->query('v') && ($maxAge = $collection->getMaxAge()) > 0) {
            $cacheControl = "max-age={$maxAge}, public";
        }
        else {
            $cacheControl = 'no-store, private';
        }

        return response(
            $svgContent,
            200,
            [
                'Content-Type' => 'image/svg+xml',
                'Cache-Control' => $cacheControl,
            ]
        );
    }

}