<?php

namespace Ingot\EventListener;

use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class NetGroupListener implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
{
    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();

        if ($ip = $request->headers->get('X_REAL_IP')) {
            $request->server->set('REMOTE_ADDR', $ip);
        }

        if ('https' == $request->headers->get('X-Forwarded-Proto')) {
            $request->server->set('HTTPS', 'on');
        }
    }

    public static function getSubscribedEvents()
    {
        return [
            // the highest the priority, the earlier a listener is executed
            KernelEvents::REQUEST => ['onKernelRequest', 255],
        ];
    }
}
