<commit_before><?php
/**
 * Copyright 2016 <NAME> <<EMAIL>>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace fkooman\OAuth\Client;

use fkooman\OAuth\Client\Exception\OAuthException;
use InvalidArgumentException;
use DomainException;

class OAuth2Client
{
    /** @var Provider */
    private $provider;

    /** @var HttpClientInterface */
    private $httpClient;

    /** @var RandomInterface */
    private $random;

    public function __construct(Provider $provider, HttpClientInterface $httpClient, RandomInterface $random = null)
    {
        $this->provider = $provider;
        $this->httpClient = $httpClient;
        if (is_null($random)) {
            $random = new Random();
        }
        $this->random = $random;
    }

    /**
     * Obtain an authorization request URL to start the authorization process
     * at the OAuth provider.
     *
     * @param string $scope       the space separated scope tokens
     * @param string $redirectUri the URL to redirect back to after coming back
     *                            from the OAuth provider (callback URL)
     *
     * @return string the authorization request URL
     */
    public function getAuthorizationRequestUri($scope, $redirectUri)
    {
        $state = $this->random->get();

        $queryParams = http_build_query(
            [
                'client_id' => $this->provider->getId(),
                'redirect_uri' => $redirectUri,
                'scope' => $scope,
                'state' => $state,
                'response_type' => 'code',
            ],
            '&'
        );

        return sprintf(
            '%s%s%s',
            $this->provider->getAuthorizationEndpoint(),
            false === strpos($this->provider->getAuthorizationEndpoint(), '?') ? '?' : '&',
            $queryParams
        );
    }

    /**
     * Obtain the access token from the OAuth provider after returning from the
     * OAuth provider on the redirectUri (callback URL).
     *
     * @param string $authorizationRequestUri    the original authorization
     *                                           request URL as obtained by getAuthorzationRequestUri
     * @param string $authorizationResponseCode  the code passed to the 'code' 
     *                                           query parameter on the callback URL
     * @param string $authorizationResponsestate the state passed to the 'state'
     *                                           query parameter on the callback URL
     *
     * @return AccessToken
     */
    public function getAccessToken($authorizationRequestUri, $authorizationResponseCode, $authorizationResponseState)
    {
        self::requireNonEmptyStrings(func_get_args());

        // parse our authorizationRequestUri to extract the state
        if (false === strpos($authorizationRequestUri, '?')) {<commit_msg>fix small documentation typo<commit_after><?php
/**
 * Copyright 2016 <NAME> <<EMAIL>>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace fkooman\OAuth\Client;

use fkooman\OAuth\Client\Exception\OAuthException;
use InvalidArgumentException;
use DomainException;

class OAuth2Client
{
    /** @var Provider */
    private $provider;

    /** @var HttpClientInterface */
    private $httpClient;

    /** @var RandomInterface */
    private $random;

    public function __construct(Provider $provider, HttpClientInterface $httpClient, RandomInterface $random = null)
    {
        $this->provider = $provider;
        $this->httpClient = $httpClient;
        if (is_null($random)) {
            $random = new Random();
        }
        $this->random = $random;
    }

    /**
     * Obtain an authorization request URL to start the authorization process
     * at the OAuth provider.
     *
     * @param string $scope       the space separated scope tokens
     * @param string $redirectUri the URL to redirect back to after coming back
     *                            from the OAuth provider (callback URL)
     *
     * @return string the authorization request URL
     */
    public function getAuthorizationRequestUri($scope, $redirectUri)
    {
        $state = $this->random->get();

        $queryParams = http_build_query(
            [
                'client_id' => $this->provider->getId(),
                'redirect_uri' => $redirectUri,
                'scope' => $scope,
                'state' => $state,
                'response_type' => 'code',
            ],
            '&'
        );

        return sprintf(
            '%s%s%s',
            $this->provider->getAuthorizationEndpoint(),
            false === strpos($this->provider->getAuthorizationEndpoint(), '?') ? '?' : '&',
            $queryParams
        );
    }

    /**
     * Obtain the access token from the OAuth provider after returning from the
     * OAuth provider on the redirectUri (callback URL).
     *
     * @param string $authorizationRequestUri    the original authorization
     *                                           request URL as obtained by getAuthorzationRequestUri
     * @param string $authorizationResponseCode  the code passed to the 'code' 
     *                                           query parameter on the callback URL
     * @param string $authorizationResponseState the state passed to the 'state'
     *                                           query parameter on the callback URL
     *
     * @return AccessToken
     */
    public function getAccessToken($authorizationRequestUri, $authorizationResponseCode, $authorizationResponseState)
    {
        self::requireNonEmptyStrings(func_get_args());

        // parse our authorizationRequestUri to extract the state
        if (false === strpos($authorizationRequestUri, '?')) {
{'fraction_non_alphanumeric': 0.09434801958166444, 'fraction_numerical': 0.0026702269692923898, 'mean_word_length': 2.2444658325312803, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4880290', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1640, 'n_words': 553}