<?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\Requests;


use LightSaml\Helper;
use LightSaml\SamlConstants;
use LightSaml\Model\Protocol\LogoutRequest;

class Logout extends AbstractRequest
{
    protected $request_id;
    protected $request;

    public function initiate()
    {
        $binding = \Buckhill\config('AuthStack.IdP.SLO.binding');

        switch($binding)
        {
            case SamlConstants::BINDING_SAML2_HTTP_POST:
                $this->initiateWithPOST();
                break;

            case SamlConstants::BINDING_SAML2_HTTP_REDIRECT:
                $this->initiateWithGET();
            default:
        }
    }


    /**
     * @return LogoutRequest
     */
    public function getRequest()
    {
        return $this->getLogout();
    }

    /**
     * @return LogoutRequest
     */
    public function getLogout()
    {
        return $this->request;
    }


    /**
     *
     */
    protected function create()
    {
        $logout = new LogoutRequest();

        $request_id = Helper::generateID();

        $logout
            ->setID($request_id)
            ->setDestination($this->getDestination())
            ->setIssueInstant(new \DateTime("now", new \DateTimeZone("UTC")))
            ->setIssuer($this->getIssuer())
        ;

        if($this->IdPWantsSignedRequests())
        {
            $logout->setSignature($this->getSignature());
        }

        $this->request = $logout;
        $this->request_id = $request_id;
    }


    /**
     * @return string
     */
    protected function getDestination()
    {
        return \Buckhill\config('AuthStack.IdP.SLO.url');
    }
}