<?php

namespace KupShop\ContentBundle\Controller;

use KupShop\ContentBundle\Util\InstagramUtil;
use KupShop\KupShopBundle\Routing\TranslatedRoute;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class InstagramController extends AbstractController
{
    /**
     * @TranslatedRoute("/_kupshop/instagram/feed")
     */
    public function feedAction(Request $request, InstagramUtil $instagramUtil)
    {
        if (!($callback = $request->query->get('callback'))) {
            throw new NotFoundHttpException('Missing callback');
        }

        $count = $request->query->get('count');

        $feed = $instagramUtil->getFeed($callback, $count);

        $response = new Response($feed);
        $response->headers->set('content-type', 'text/javascript; charset=utf-8');
        $response->setClientTtl(3600);
        $response->setExpires(new \DateTime('now + 1 hour'));

        return $response;
    }
}
