<?php

/**
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; under version 2
 * of the License (non-upgradable).
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Copyright (c) 2019 (original work) Open Assessment Technologies SA;
 */

declare(strict_types=1);

namespace App\EventSubscriber;

use App\Action\Api\ApiActionInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Twig\Environment;

class ErrorHandlerSubscriber implements EventSubscriberInterface
{
    /** @var Environment */
    private $twig;

    /** @var LoggerInterface */
    private $logger;

    public function __construct(Environment $twig, LoggerInterface $logger)
    {
        $this->twig = $twig;
        $this->logger = $logger;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::EXCEPTION => 'onKernelException',
        ];
    }

    public function onKernelException(ExceptionEvent $event): void
    {
        $request = $event->getRequest();
        $exception = $event->getThrowable();

        $this->logger->error($exception->getMessage());

        $code = $exception instanceof HttpExceptionInterface
            ? $exception->getStatusCode()
            : Response::HTTP_INTERNAL_SERVER_ERROR;

        $controller = $request->attributes->get('_controller');

        if (is_a($controller, ApiActionInterface::class, true) || 0 === strpos($request->getPathInfo(), '/api/')) {

            $errorMessage = null === $controller
                ? sprintf('Api error: %s', $exception->getMessage())
                : sprintf('%s api error: %s', $controller::getName(), $exception->getMessage());

            $event->setResponse(
                new JsonResponse(
                    [
                        'error' => $errorMessage,
                    ],
                    $code
                )
            );
        } else {
            if ($request->isXmlHttpRequest()) {
                $event->setResponse(
                    new JsonResponse(
                        [
                            'code' => $code,
                            'message' => $exception->getMessage(),
                        ]
                    )
                );
            } else {
                $event->setResponse(
                    new Response(
                        $this->twig->render(
                            'error/error.html.twig',
                            [
                                'code' => $code,
                                'message' => $exception->getMessage(),
                            ]
                        )
                    )
                );
            }
        }
    }
}
