function it_logs_in_after_responses_that_required_login_and_retries_the_request(SiteConfig $siteConfig, Factory $authenticatorFactory, CompleteEvent $completeEvent, Authenticator $authenticator, ClientInterface $guzzle) { $siteConfig->requiresLogin()->willReturn(true); $authenticatorFactory->buildFromSiteConfig($siteConfig)->willReturn($authenticator); $authenticator->isLoginRequired(Argument::type('string'))->willReturn(true); $authenticator->login($guzzle)->shouldBeCalled(); $completeEvent->retry()->shouldBeCalled(); $this->loginIfRequested($completeEvent); }
public function loginIfRequested(CompleteEvent $event) { if (($config = $this->buildSiteConfig($event->getRequest())) === false) { return; } if (!$config->requiresLogin()) { return; } $authenticator = $this->authenticatorFactory->buildFromSiteConfig($config); if ($authenticator->isLoginRequired($event->getResponse()->getBody())) { $client = $event->getClient(); $emitter = $client->getEmitter(); $emitter->detach($this); $authenticator->login($client); $emitter->attach($this); $event->retry(); } }
/** * Called when a request receives a redirect response * * @param CompleteEvent $event Event emitted * @throws TooManyRedirectsException */ public function onComplete(CompleteEvent $event) { $response = $event->getResponse(); if (substr($response->getStatusCode(), 0, 1) != '3' || !$response->hasHeader('Location')) { return; } $request = $event->getRequest(); $config = $request->getConfig(); // Increment the redirect and initialize the redirect state. if ($redirectCount = $config['redirect_count']) { $config['redirect_count'] = ++$redirectCount; } else { $config['redirect_scheme'] = $request->getScheme(); $config['redirect_count'] = $redirectCount = 1; } $max = $config->getPath('redirect/max') ?: 5; if ($redirectCount > $max) { throw new TooManyRedirectsException("Will not follow more than {$redirectCount} redirects", $request); } $this->modifyRedirectRequest($request, $response); $event->retry(); }