KnpU\OAuth2ClientBundle\Client\OAuth2Client::redirect PHP Method

redirect() public method

Creates a RedirectResponse that will send the user to the OAuth2 server (e.g. send them to Facebook).
public redirect ( array $scopes = [] ) : RedirectResponse
$scopes array The scopes you want (leave empty to use default)
return Symfony\Component\HttpFoundation\RedirectResponse
    public function redirect(array $scopes = [])
    {
        $options = [];
        if (!empty($scopes)) {
            $options['scope'] = $scopes;
        }
        $url = $this->provider->getAuthorizationUrl($options);
        // set the state (unless we're stateless)
        if (!$this->isStateless) {
            $this->getSession()->set(self::OAUTH2_SESSION_STATE_KEY, $this->provider->getState());
        }
        return new RedirectResponse($url);
    }

Usage Example

 public function testRedirectWithoutState()
 {
     $requestStack = $this->prophesize('Symfony\\Component\\HttpFoundation\\RequestStack');
     $requestStack->getCurrentRequest()->shouldNotBeCalled();
     $this->provider->getAuthorizationUrl([])->willReturn('http://example.com');
     $client = new OAuth2Client($this->provider->reveal(), $requestStack->reveal());
     $client->setAsStateless();
     $response = $client->redirect();
     // don't need other checks - the fact that it didn't fail
     // by asking for the request and session is enough
     $this->assertInstanceOf('Symfony\\Component\\HttpFoundation\\RedirectResponse', $response);
 }