<?php

namespace External\MSSQLBundle\Controller;

use External\MSSQLBundle\Util\SynchronizerLocator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends AbstractController
{
    /**
     * @Route("/_money/{type}/")
     */
    public function syncAction(Request $request, SynchronizerLocator $locator, $type = null)
    {
        return new StreamedResponse(function () use ($type, $locator, $request) {
            ini_set('max_execution_time', (60 * 60) * 4);
            while (ob_get_level()) {
                ob_end_flush();
            }

            $synchronizer = $locator->getServiceByType($type);
            if ($request->get('fullSync')) {
                $synchronizer->setFullSync(true);
            }
            $synchronizer->sync();
        });
    }
}
