<?php

namespace AuthStack\SP\Responses;


use LightSaml\Helper;
use LightSaml\SamlConstants;
use LightSaml\Model\Protocol\Response;
use LightSaml\Model\Protocol\LogoutResponse;

/**
 * Class Logout
 * @package AuthStack\SP\Responses
 */
class Logout extends AbstractResponse
{
    public function response()
    {
        $binding = \Buckhill\config('AuthStack.IdP.SLO.binding');

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


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


    /**
     * @param Response $response
     */
    protected function create(Response $response)
    {
        $logout = new LogoutResponse();

        $request_id = Helper::generateID();

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

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

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


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

}