<?php

/*
 * This file is part of the AuthStack project.
 *
 * (c) Buckhill Ltd <development@buckhill.co.uk>
 *
 * This source file is subject to the Buckhill license that is bundled
 * with this source code in the file LICENSE.
 */

namespace AuthStack\SP;

use AuthStack\SP\Helpers\AuthN;
use AuthStack\SP\Helpers\Logout;
use LightSaml\Binding\BindingFactory;
use Symfony\Component\HttpFoundation\Request;
use LightSaml\Context\Profile\MessageContext;


if(!function_exists(__NAMESPACE__ . '\authn'))
{
    function authn(array $config, $idp, Request $request = null)
    {
        return new AuthN($config, $idp, $request);
    }
}

if(!function_exists(__NAMESPACE__ .'\logout'))
{
    function logout(array $config, $idp, $relay_state = null)
    {
        $logout = new Logout($config, $idp);

        //$logout->setRelayState($relay_state);

        return $logout;
    }
}

if(!function_exists(__NAMESPACE__ . '\metadata'))
{
    function metadata(array $config)
    {
        return new Metadata($config);
    }
}

if(!function_exists(__NAMESPACE__ . '\receive'))
{
    /**
     * @param Request|null $request
     * @return \LightSaml\Model\Protocol\SamlMessage
     */
    function receive(Request $request = null)
    {
        if(is_null($request))
        {
            $request = Request::createFromGlobals();
        }

        $factory = new BindingFactory();
        $context = new MessageContext();

        $binding = $factory->getBindingByRequest($request);

        $binding->receive($request, $context);

        return $context->getMessage();
    }
}
