<commit_before><?php

namespace Authoritarian\Flow;

use Authoritarian\Exception\Flow\MissingAuthorizationCodeException;
use Authoritarian\Credential\ClientCredential;

/**
 * Implementation of Authorization Code Flow
 **/
class AuthorizationCodeFlow extends AbstractFlow
{
    const GRANT_TYPE = 'authorization_code';
    const RESPONSE_TYPE = 'code';

    protected $authorizeUrl;
    protected $code;
    protected $redirectUri;
    protected $state;
    protected $parameters;

    /**
     * @param string $authorize_url The OAuth Authorize endpoint url
     */
    public function __construct($authorize_url)
    {
        $this->authorizeUrl = $authorize_url;
    }

    /**
     * @param string $code The authorization code retrieved in the callback page
     */
    public function setCode($code)
    {
        $this->code = $code;
    }

    /**
     * @param string $uri the callback URI to retrieve the authorization code
     */
    public function setRedirectUri($uri)
    {
        $this->redirectUri = $uri;
    }

    /**
     * @param string $state The app's state to be resumed at the callback
     */
    public function setState($state)
    {
        $this->state = $state;
    }

    /**
     * Get the URL to user's authentication and authorization
     *
     * @return string
     */
    public function getAuthUrl()
    {
        return $this->authorizeUrl . '?' . $this->getAuthorizeQueryParameters();
    }

    /**
     * {@inheritDoc}
     * @throws MissingAuthorizationCodeException When the authorization code
     * wasn't set
     */
    public function getRequest()
    {
        parent::getRequest();

        if (is_null($this->code)) {
            throw new MissingAuthorizationCodeException(
                'No authorization code given to generate a request'
            );
        }

        return $this->client->post(
            $this->tokenUrl,
            null,
            $this->getRequestPostParameters()
        );
    }

    private function getRequestPostParameters()
    {
        $parameters = array(
            'code' => $this->code,
            'client_id' => $this->clientId,
            'client_secret' => $this->clientSecret,
            'grant_type' => self::GRANT_TYPE,
            'redirect_uri' => $this->redirectUri,
            'scope' => $this->scope,
        );

        return $this->removeNullItems($parameters);
    }

    private function getAuthorizeQueryParameters()
    {
        $parameters = array(
            'redirect_uri' => $this->redirectUri,
            'client_id' => $this->clientId,
            'response_type' => self::RESPONSE_TYPE,
            'scope' => $this->scope,
            'state' => $this->state,
        );

        return http_build_query(
            $this->removeNullItems($parameters)
        );
    }
}
<commit_msg>simplify AuthorizationCodeFlow<commit_after><?php

namespace Authoritarian\Flow;

use Authoritarian\Exception\Flow\MissingAuthorizationCodeException;
use Authoritarian\Credential\ClientCredential;

/**
 * Implementation of Authorization Code Flow
 **/
class AuthorizationCodeFlow extends AbstractFlow
{
    const GRANT_TYPE = 'authorization_code';
    const RESPONSE_TYPE = 'code';

    protected $authorizationUrl;
    protected $code;
    protected $redirectUri;
    protected $state;
    protected $parameters;

    /**
     * @param string $authorization_url OAuth 2's Authorization endpoint url
     */
    public function __construct($authorization_url = null)
    {
        $this->setAuthorizationUrl($authorization_url);
    }

    /**
     * @param string $authorization_url OAuth 2's Authorization endpoint url
     */
    public function setAuthorizationUrl($authorization_url)
    {
        $this->authorizationUrl = $authorization_url;
    }

    /**
     * @param string $code The authorization code retrieved in the callback page
     */
    public function setCode($code)
    {
        $this->code = $code;
    }

    /**
     * @param string $uri the callback URI to retrieve the authorization code
     */
    public function setRedirectUri($uri)
    {
        $this->redirectUri = $uri;
    }

    /**
     * @param string $state The app's state to be resumed at the callback
     */
    public function setState($state)
    {
        $this->state = $state;
    }

    /**
     * Get the URL to user's authentication and authorization
     *
     * @return string
     */
    public function getAuthUrl()
    {
        return $this->authorizationUrl . '?' . $this->getAuthorizeQueryParameters();
    }

    /**
     * {@inheritDoc}
     * @throws MissingAuthorizationCodeException When the authorization code
     * wasn't set
     */
    public function getRequest()
    {
        parent::getRequest();

        if (is_null($this->code)) {
            throw new MissingAuthorizationCodeException(
                'No authorization code given to generate a request'
            );
        }

        return $this->client->post(
            $this->tokenUrl,
            null,
            $this->getRequestPostParameters()
        );
    }

    private function getRequestPostParameters()
    {
        $parameters = array(
            'code' => $this->code,
            'client_id' => $this->clientId,
            'client_secret' => $this->clientSecret,
            'grant_type' => self::GRANT_TYPE,
            'redirect_uri' => $this->redirectUri,
            'scope' => $this->scope,
        );

        return $this->removeNullItems($parameters);
    }

    private function getAuthorizeQueryParameters()
    {
        $parameters = array(
            'redirect_uri' => $this->redirectUri,
            'client_id' => $this->clientId,
            'response_type' => self::RESPONSE_TYPE,
            'scope' => $this->scope,
            'state' => $this->state,
        );

        return http_build_query(
            $this->removeNullItems($parameters)
        );
    }
}

{'fraction_non_alphanumeric': 0.11210686506594522, 'fraction_numerical': 0.00033818058843422386, 'mean_word_length': 2.195569962182604, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2360829', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1594, 'n_words': 370}