/**
  * @param \Symfony\Component\HttpFoundation\Request $request
  * @throws \InvalidArgumentException if cannot manage the Request
  * @return \Symfony\Component\HttpFoundation\Response|SamlSpInfo
  */
 public function manage(Request $request)
 {
     if (false == $this->supports($request)) {
         throw new \InvalidArgumentException('Unsupported request');
     }
     $serviceInfo = $this->serviceInfoCollection->findByAS($request->query->get('as'));
     if (!$serviceInfo) {
         return new RedirectResponse($this->httpUtils->generateUri($request, $request->attributes->get('discovery_path')));
     }
     $serviceInfo->getSpProvider()->setRequest($request);
     $spED = $serviceInfo->getSpProvider()->getEntityDescriptor();
     $idpED = $serviceInfo->getIdpProvider()->getEntityDescriptor();
     $spMeta = $serviceInfo->getSpMetaProvider()->getSpMeta();
     $builder = new AuthnRequestBuilder($spED, $idpED, $spMeta);
     $message = $builder->build();
     if ($serviceInfo->getSpSigningProvider()->isEnabled()) {
         $message->sign($serviceInfo->getSpSigningProvider()->getCertificate(), $serviceInfo->getSpSigningProvider()->getPrivateKey());
     }
     $binding = $this->bindingManager->instantiate($spMeta->getAuthnRequestBinding());
     $bindingResponse = $binding->send($message);
     if ($bindingResponse instanceof \AerialShip\LightSaml\Binding\RedirectResponse) {
         $result = new RedirectResponse($bindingResponse->getDestination());
     } else {
         if ($bindingResponse instanceof \AerialShip\LightSaml\Binding\PostResponse) {
             $result = new Response($bindingResponse->render());
         } else {
             throw new \RuntimeException('Unrecognized binding response ' . get_class($bindingResponse));
         }
     }
     $state = new RequestState();
     $state->setId($message->getID());
     $state->setDestination($serviceInfo->getIdpProvider()->getEntityDescriptor()->getEntityID());
     $this->requestStore->set($state);
     return $result;
 }
 /**
  * @param \Symfony\Component\HttpFoundation\Request $request
  * @throws \Symfony\Component\Process\Exception\RuntimeException
  * @return \Symfony\Component\HttpFoundation\Response|SamlSpInfo
  */
 function manage(Request $request)
 {
     $serviceInfo = $this->serviceInfoCollection->findByAS($request->query->get('as'));
     if (!$serviceInfo) {
         return $this->httpUtils->createRedirectResponse($request, $request->attributes->get('discovery_path') . '?type=metadata');
     }
     $serviceInfo->getSpProvider()->setRequest($request);
     $ed = $serviceInfo->getSpProvider()->getEntityDescriptor();
     $context = new SerializationContext();
     $ed->getXml($context->getDocument(), $context);
     $result = new Response($context->getDocument()->saveXML());
     $result->headers->set('Content-Type', 'application/samlmetadata+xml');
     return $result;
 }
 /**
  * @test
  */
 public function shouldReturnServiceInfoWhenFindByAsIsCalledWithIdpIDAndThereIsMoreThenOne()
 {
     $col = new ServiceInfoCollection();
     $expectedServiceInfo_1 = $this->createServiceInfoStub($expectedProviderID_1 = 'main', $expectedIdpID_1 = 'idp1', null, null, null, null);
     $expectedServiceInfo_2 = $this->createServiceInfoStub($expectedProviderID_2 = 'main', $expectedIdpID_2 = 'idp2', null, null, null, null);
     $col->add($expectedServiceInfo_1);
     $col->add($expectedServiceInfo_2);
     $this->assertEquals($expectedServiceInfo_1, $col->findByAS($expectedIdpID_1));
     $this->assertEquals($expectedServiceInfo_2, $col->findByAS($expectedIdpID_2));
 }