/**
  * @param \Symfony\Component\HttpFoundation\Request $request
  * @param \Atrauzzi\Oauth2Server\Domain\Entity\Oauthable $oauthable
  * @return string
  * @throws \Atrauzzi\Oauth2Server\Exception\InvalidClient
  * @throws \Atrauzzi\Oauth2Server\Exception\InvalidCredentials
  * @throws \Atrauzzi\Oauth2Server\Exception\InvalidRequest
  * @throws \Atrauzzi\Oauth2Server\Exception\InvalidScope
  * @throws \Atrauzzi\Oauth2Server\Exception\UnsupportedResponseType
  */
 protected function doAuthorizeFlow(Request $request, Oauthable $oauthable)
 {
     if (!$oauthable) {
         throw new InvalidCredentials();
     }
     if (!($clientId = $request->query->get('client_id'))) {
         throw new InvalidRequest('client_id');
     }
     if (!($redirectUri = $request->query->get('redirect_uri'))) {
         throw new InvalidRequest('redirect_uri');
     }
     $state = $request->query->get('state');
     if ($this->config->stateParamRequired() && !$state) {
         throw new InvalidRequest('state', $redirectUri);
     }
     if (!($responseType = $request->query->get('response_type'))) {
         throw new InvalidRequest('response_type', $redirectUri);
     }
     if ($responseType != $this->getResponseType()) {
         throw new UnsupportedResponseType($responseType, $redirectUri);
     }
     $client = $this->clientRepository->find($clientId, null, $this->getIdentifier(), $redirectUri);
     if (!$client instanceof Client) {
         throw new InvalidClient();
     }
     if ($this->config->requireRedirectDomainMatch() && !$this->validateRedirectUri($client->getRedirectUri(), $redirectUri)) {
         throw new InvalidRequest('redirect_uri', $redirectUri);
     }
     //
     //
     $scopes = $this->scopeService->findValid($request->query->get('scope'), $this->getIdentifier(), $client->getId(), $redirectUri);
     $authCode = $this->authorizationCodeRepository->create(SecureKey::generate(), time() + $this->config->getAuthorizationCodeTtl(), $oauthable->getId(), $oauthable->getType(), $client->getId(), $scopes ? array_keys($scopes) : null, $request->get('redirect_uri'));
     $this->authorizationCodeRepository->persist($authCode);
     return ['authorization_code' => $authCode, 'redirect_uri' => $authCode->generateRedirectUri($state)];
 }