<commit_before><?php

/**
 * This file is part of Krizalys' OneDrive SDK for PHP.
 *
 * For the full copyright and license information, please view the LICENSE file
 * that was distributed with this source code.
 *
 * @author    <NAME>
 * @copyright 2008-2019 <NAME> (http://www.krizalys.com)
 * @license   https://opensource.org/licenses/BSD-3-Clause 3-Clause BSD License
 * @link      https://github.com/krizalys/onedrive-php-sdk
 */

namespace Krizalys\Onedrive;

use GuzzleHttp\ClientInterface;
use Krizalys\Onedrive\Constant\AccessTokenStatus;
use Krizalys\Onedrive\Constant\SpecialFolderName;
use Krizalys\Onedrive\Definition\ServiceDefinitionInterface;
use Krizalys\Onedrive\Proxy\DriveItemProxy;
use Krizalys\Onedrive\Proxy\DriveProxy;
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;

/**
 * A client interface to communicate with the OneDrive API.
 *
 * `Client` instances act as entry points allowing client applications to
 * perform currently implemented OneDrive operations programmatically.
 *
 * For example, assuming you have instantiated a client and the user has logged
 * successfully and authorized your client application:
 *
 * ```php
 * $client->getRoot()->upload('hello.txt', 'Hello World!');
 * ```
 *
 * Applications are managed via Microsoft accounts. Two types of applications
 * are supported:
 *   - Microsoft identity platform (v2.0) applications, recommended for new
 *     applications; see
 *     {@link https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade "App registrations" in Microsoft Azure} ;
 *   - Live SDK applications, deprecated; see
 *     {@link https://apps.dev.microsoft.com/#/appList "My applications" in Microsoft
 *     Application Registration Portal}.
 *
 * @since 1.0.0
 *
 * @api
 */
class Client
{
    /**
     * @var string
     *      The base URL for authorization requests.
     */
    const AUTH_URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';

    /**
     * @var string
     *      The base URL for token requests.
     */
    const TOKEN_URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';

    /**
     * @var string
     *      The legacy date/time format.
     *
     * @deprecated 2.0.0 Non-standard format. Use ISO-8601 date/times instead.
     */
    const LEGACY_DATETIME_FORMAT = 'Y-m-d\TH:i:sO';

    /**
     * @var string
     *      The client ID.
     */
    private $clientId;

    /**
     * @var \Microsoft\Graph\Graph
     *      The Microsoft Graph.
     */
    private $graph;

    /**
     * @var \GuzzleHttp\ClientInterface
     *      The Guzzle HTTP client.
     */
    private $httpClient;

    /**
     * @var \Krizalys\Onedrive\Definition\ServiceDefinitionInterface
     *      The service definition.
     */
    private $serviceDefinition;

    /**
     * @var object
     *      The OAuth state (token, etc...).
     */
    private $_state;

    /**
     * Constructor.
     *
     * @param string $clientId
     *        The client ID.
     * @param \Microsoft\Graph\Graph $graph
     *        The Microsoft Graph.
     * @param \GuzzleHttp\ClientInterface $httpClient
     *        The Guzzle HTTP client.
     * @param mixed $serviceDefinition
     *        The service definition. Not passing a
     *        \Krizalys\Onedrive\Definition\ServiceDefinitionInterface via this
     *        parameter is deprecated and will be disallowed in version 3.
     *        Passing a logger via this parameter is deprecated and will be
     *        disallowed in version 3.
     * @param mixed[string] $options
     *        The options to use while creating this object. Supported options:
     *          - `'state'` *(object)*: the OneDrive client state, as returned
     *            by `getState()`. Default: `[]`.
     *
     * @throws \Exception
     *         Thrown if `$clientId` is `null`.
     *
     * @since 1.0.0
     */
    public function __construct(
        $clientId,
        Graph $graph,
        ClientInterface $httpClient,
        $serviceDefinition = null,
        array $options = []
    ) {
        switch (func_num_args()) {
            case 3:
                $serviceDefinition = null;
                break;

            case 4:
                if (is_array($serviceDefinition)) {
                    $options           = $serviceDefinition;
                    $logger            = null;
                    $serviceDefinition = null;
                } elseif ($serviceDefinition instanceof ServiceDefinitionInterface) {
                    $logger = null;
                } else {
                    $logger            = $serviceDefinition;
                    $serviceDefinition = null;
                }

                break;

            case 5:
                if ($serviceDefinition instanceof ServiceDefinitionInterface) {
                    $logger = null;
                } else {
                    $logger            = $serviceDefinition;
                    $serviceDefinition = null;
                }

                break;
        }

        if ($serviceDefinition === null) {
            $message = 'Not passing a'
                . ' \Krizalys\Onedrive\Definition\ServiceDefinitionInterface'
                . ' instance via $serviceDefinition is deprecated and will be'
                . ' disallowed in version 3; pass this parameter';

            @trigger_error($message, E_USER_DEPRECATED);

            $serviceDefinition = Onedrive::buildServiceDefinition();
        }

        if ($logger !== null) {
            $message = 'Passing a logger via $serviceDefinition is deprecated'
                . ' and will be disallowed in version 3; omit this parameter,'
                . ' or pass null or options instead';

            @trigger_error($message, E_USER_DEPRECATED);
        }

        if ($clientId === null) {
            throw new \Exception('The client ID must be set');
        }

        $this->clientId          = $clientId;
        $this->graph             = $graph;
        $this->httpClient        = $httpClient;
        $this->serviceDefinition = $serviceDefinition;

        $this->_state = array_key_exists('state', $options)
            ? $options['state'] : (object) [
                'redirect_uri' => null,
                'token'        => null,
            ];
    }

    /**<commit_msg>Set Access Token in Client Class Constructor<commit_after><?php

/**
 * This file is part of Krizalys' OneDrive SDK for PHP.
 *
 * For the full copyright and license information, please view the LICENSE file
 * that was distributed with this source code.
 *
 * @author    <NAME>
 * @copyright 2008-2019 <NAME> (http://www.krizalys.com)
 * @license   https://opensource.org/licenses/BSD-3-Clause 3-Clause BSD License
 * @link      https://github.com/krizalys/onedrive-php-sdk
 */

namespace Krizalys\Onedrive;

use GuzzleHttp\ClientInterface;
use Krizalys\Onedrive\Constant\AccessTokenStatus;
use Krizalys\Onedrive\Constant\SpecialFolderName;
use Krizalys\Onedrive\Definition\ServiceDefinitionInterface;
use Krizalys\Onedrive\Proxy\DriveItemProxy;
use Krizalys\Onedrive\Proxy\DriveProxy;
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;

/**
 * A client interface to communicate with the OneDrive API.
 *
 * `Client` instances act as entry points allowing client applications to
 * perform currently implemented OneDrive operations programmatically.
 *
 * For example, assuming you have instantiated a client and the user has logged
 * successfully and authorized your client application:
 *
 * ```php
 * $client->getRoot()->upload('hello.txt', 'Hello World!');
 * ```
 *
 * Applications are managed via Microsoft accounts. Two types of applications
 * are supported:
 *   - Microsoft identity platform (v2.0) applications, recommended for new
 *     applications; see
 *     {@link https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade "App registrations" in Microsoft Azure} ;
 *   - Live SDK applications, deprecated; see
 *     {@link https://apps.dev.microsoft.com/#/appList "My applications" in Microsoft
 *     Application Registration Portal}.
 *
 * @since 1.0.0
 *
 * @api
 */
class Client
{
    /**
     * @var string
     *      The base URL for authorization requests.
     */
    const AUTH_URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';

    /**
     * @var string
     *      The base URL for token requests.
     */
    const TOKEN_URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';

    /**
     * @var string
     *      The legacy date/time format.
     *
     * @deprecated 2.0.0 Non-standard format. Use ISO-8601 date/times instead.
     */
    const LEGACY_DATETIME_FORMAT = 'Y-m-d\TH:i:sO';

    /**
     * @var string
     *      The client ID.
     */
    private $clientId;

    /**
     * @var \Microsoft\Graph\Graph
     *      The Microsoft Graph.
     */
    private $graph;

    /**
     * @var \GuzzleHttp\ClientInterface
     *      The Guzzle HTTP client.
     */
    private $httpClient;

    /**
     * @var \Krizalys\Onedrive\Definition\ServiceDefinitionInterface
     *      The service definition.
     */
    private $serviceDefinition;

    /**
     * @var object
     *      The OAuth state (token, etc...).
     */
    private $_state;

    /**
     * Constructor.
     *
     * @param string $clientId
     *        The client ID.
     * @param \Microsoft\Graph\Graph $graph
     *        The Microsoft Graph.
     * @param \GuzzleHttp\ClientInterface $httpClient
     *        The Guzzle HTTP client.
     * @param mixed $serviceDefinition
     *        The service definition. Not passing a
     *        \Krizalys\Onedrive\Definition\ServiceDefinitionInterface via this
     *        parameter is deprecated and will be disallowed in version 3.
     *        Passing a logger via this parameter is deprecated and will be
     *        disallowed in version 3.
     * @param mixed[string] $options
     *        The options to use while creating this object. Supported options:
     *          - `'state'` *(object)*: the OneDrive client state, as returned
     *            by `getState()`. Default: `[]`.
     *
     * @throws \Exception
     *         Thrown if `$clientId` is `null`.
     *
     * @since 1.0.0
     */
    public function __construct(
        $clientId,
        Graph $graph,
        ClientInterface $httpClient,
        $serviceDefinition = null,
        array $options = []
    ) {
        switch (func_num_args()) {
            case 3:
                $serviceDefinition = null;
                break;

            case 4:
                if (is_array($serviceDefinition)) {
                    $options           = $serviceDefinition;
                    $logger            = null;
                    $serviceDefinition = null;
                } elseif ($serviceDefinition instanceof ServiceDefinitionInterface) {
                    $logger = null;
                } else {
                    $logger            = $serviceDefinition;
                    $serviceDefinition = null;
                }

                break;

            case 5:
                if ($serviceDefinition instanceof ServiceDefinitionInterface) {
                    $logger = null;
                } else {
                    $logger            = $serviceDefinition;
                    $serviceDefinition = null;
                }

                break;
        }

        if ($serviceDefinition === null) {
            $message = 'Not passing a'
                . ' \Krizalys\Onedrive\Definition\ServiceDefinitionInterface'
                . ' instance via $serviceDefinition is deprecated and will be'
                . ' disallowed in version 3; pass this parameter';

            @trigger_error($message, E_USER_DEPRECATED);

            $serviceDefinition = Onedrive::buildServiceDefinition();
        }

        if ($logger !== null) {
            $message = 'Passing a logger via $serviceDefinition is deprecated'
                . ' and will be disallowed in version 3; omit this parameter,'
                . ' or pass null or options instead';

            @trigger_error($message, E_USER_DEPRECATED);
        }

        if ($clientId === null) {
            throw new \Exception('The client ID must be set');
        }

        $this->clientId          = $clientId;
        $this->graph             = $graph;
        $this->httpClient        = $httpClient;
        $this->serviceDefinition = $serviceDefinition;

        $this->_state = array_key_exists('state', $options)
            ? $options['state'] : (object) [
                'redirect_uri' => null,
                'token'        => null,
            ];
        
        if (null !== $this->_state->token) {
            $this->graph->setAccessToken($this->_state->token->data->access_token);
        }
    }

    /**
{'fraction_non_alphanumeric': 0.10355559013459893, 'fraction_numerical': 0.00591301641640084, 'mean_word_length': 1.8996165125197384, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 33, 'https://': 12, 'lorem ipsum': 0, 'www.': 2, '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': '1403276', 'n_tokens_mistral': 3574, 'n_tokens_neox': 3397, 'n_words': 936}