<?php

namespace Alchemy\RestBundle\EventListener;

use Alchemy\Rest\Request\ContentTypeMatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class DecodeJsonBodyRequestListener implements EventSubscriberInterface
{
    /**
     * @var ContentTypeMatcher
     */
    private $contentTypeMatcher;

    /**
     * @var string[]
     */
    private $contentTypes;

    /**
     * @param ContentTypeMatcher $contentTypeMatcher
     * @param null|string[] $contentTypes
     */
    public function __construct(ContentTypeMatcher $contentTypeMatcher, array $contentTypes = null)
    {
        $this->contentTypeMatcher = $contentTypeMatcher;
        $this->contentTypes = $contentTypes ?: array('application/json');
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();

        if (! $request->attributes->get('_rest[decode_request]', false, true)) {
            return;
        }

        $this->decodeBody($request);
    }

    /**
     * @param Request $request
     */
    public function decodeBody(Request $request)
    {
        $acceptHeader = $request->headers->get('Content-Type', '*/*');

        if (empty($acceptHeader) || !$this->contentTypeMatcher->matches($acceptHeader, $this->contentTypes)) {
            return;
        }

        $jsonBody = $request->getContent(false);
        $decodedBody = json_decode($jsonBody, true);

        if (! is_array($decodedBody)) {
            $decodedBody = array();
        }

        $request->request->replace($decodedBody);
    }

    public static function getSubscribedEvents()
    {
        return array(KernelEvents::REQUEST => array('onKernelRequest', -2048));
    }
}
