/**
  * @param Message $message
  * @return \AerialShip\LightSaml\Binding\Response
  */
 public function send(Message $message)
 {
     $bindingType = $this->spMeta->getAuthnRequestBinding();
     if ($bindingType) {
         $detector = new BindingDetector();
         $binding = $detector->instantiate($bindingType);
     } else {
         $binding = new HttpRedirect();
     }
     $result = $binding->send($message);
     return $result;
 }
 /**
  * @param \Symfony\Component\HttpFoundation\Request $request
  * @throws \RuntimeException
  * @throws \InvalidArgumentException if cannot manage the Request
  * @return \Symfony\Component\HttpFoundation\Response|SamlSpInfo|null
  */
 public function manage(Request $request)
 {
     if (!$this->supports($request)) {
         throw new \InvalidArgumentException('Unsupported request');
     }
     $logoutRequest = $this->receiveRequest($request);
     $serviceInfo = $this->getServiceInfo($logoutRequest, $request);
     $this->validateLogoutRequest($serviceInfo, $logoutRequest);
     $arrStates = $this->getSSOState($serviceInfo, $logoutRequest->getNameID()->getValue(), $logoutRequest->getSessionIndex());
     $this->deleteSSOState($arrStates);
     $logoutResponse = new LogoutResponse();
     $logoutResponse->setID(Helper::generateID());
     $logoutResponse->setIssuer($serviceInfo->getSpProvider()->getEntityDescriptor()->getEntityID());
     $logoutResponse->setInResponseTo($logoutRequest->getID());
     $logoutResponse->setRelayState($logoutRequest->getRelayState());
     $arrSLO = $serviceInfo->getIdpProvider()->getEntityDescriptor()->getFirstIdpSsoDescriptor()->findSingleLogoutServices();
     /** @var  $slo SingleLogoutService */
     $slo = array_pop($arrSLO);
     $logoutResponse->setDestination($slo->getLocation());
     $status = new Status();
     $status->setSuccess();
     $logoutResponse->setStatus($status);
     // Log the user out
     $request->getSession()->invalidate();
     $this->securityContext->setToken(null);
     // Return the response to SLO Service
     $bindingType = $serviceInfo->getSpMetaProvider()->getSpMeta()->getResponseBinding();
     if ($bindingType) {
         $detector = new BindingDetector();
         $binding = $detector->instantiate($bindingType);
     } else {
         $binding = new HttpRedirect();
     }
     $bindingResponse = $binding->send($logoutResponse);
     if ($bindingResponse instanceof PostResponse) {
         return new Response($bindingResponse->render());
     } else {
         if ($bindingResponse instanceof RedirectResponse) {
             return new RedirectResponse($bindingResponse->getDestination());
         } else {
             $context = new SerializationContext();
             $logoutResponse->getXml($context->getDocument(), $context);
             $xml = $context->getDocument()->saveXML();
             return new Response($xml, 200, array('Content-Type' => 'application/xml'));
         }
     }
 }