<?php

namespace Lc\SovBundle\Controller\Dashboard;

use Lc\SovBundle\Controller\ControllerTrait;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class CommandAdminController extends AbstractController
{
    use ControllerTrait;

    /**
     * @Route("/admin/command/cache/clear", name="admin_command_cache_clear")
     */
    public function cacheClear(Request $request)
    {
        $this->doCommand('cache:clear');

        $this->addFlashTranslator('success', 'command.cacheClear');

        return $this->redirect($request->headers->get('referer'));
    }

    private function doCommand($command)
    {
        $kernel = $this->getKernel();
        $env = $kernel->getEnvironment();

        $application = new Application($kernel);
        $application->setAutoExit(false);

        $input = new ArrayInput(array(
            'command' => $command,
            '--env' => $env
        ));

        $output = new BufferedOutput();
        $application->run($input, $output);

        $content = $output->fetch();

        //return new Response($content);
    }
}
