<commit_before><?php

namespace Platformsh\Cli\Service;

use CommerceGuys\Guzzle\Oauth2\AccessToken;
use Doctrine\Common\Cache\CacheProvider;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Event\ErrorEvent;
use GuzzleHttp\Exception\BadResponseException;
use Platformsh\Cli\CredentialHelper\Manager;
use Platformsh\Cli\CredentialHelper\SessionStorage;
use Platformsh\Cli\Event\EnvironmentsChangedEvent;
use Platformsh\Cli\Model\Route;
use Platformsh\Cli\Util\NestedArrayUtil;
use Platformsh\Client\Connection\Connector;
use Platformsh\Client\Exception\ApiResponseException;
use Platformsh\Client\Model\Deployment\EnvironmentDeployment;
use Platformsh\Client\Model\Environment;
use Platformsh\Client\Model\Organization\Organization;
use Platformsh\Client\Model\Project;
use Platformsh\Client\Model\ProjectAccess;
use Platformsh\Client\Model\ProjectStub;
use Platformsh\Client\Model\Resource as ApiResource;
use Platformsh\Client\Model\SshKey;
use Platformsh\Client\Model\User;
use Platformsh\Client\PlatformClient;
use Platformsh\Client\Session\SessionInterface;
use Platformsh\Client\Session\Storage\File;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 * Decorates the PlatformClient API client to provide aggressive caching.
 */
class Api
{
    /** @var EventDispatcherInterface */
    public $dispatcher;

    /** @var Config */
    private $config;

    /** @var \Doctrine\Common\Cache\CacheProvider */
    private $cache;

    /** @var OutputInterface */
    private $output;

    /** @var OutputInterface */
    private $stdErr;

    /** @var TokenConfig */
    private $tokenConfig;

    /**
     * The library's API client object.
     *
     * This is static so that a freshly logged-in client can then be reused by a parent command with a different service container.
     *
     * @var PlatformClient
     */
    private static $client;

    /**
     * A cache of environments lists, keyed by project ID.
     *
     * @var string<Environment[]>
     */
    private static $environmentsCache = [];

    /**
     * A cache of account details arrays, keyed by project ID.
     *
     * @see Api::getAccount()
     *
     * @var string<array>
     */
    private static $accountsCache = [];

    /**
     * A cache of project access lists, keyed by project ID.
     *
     * @see Api::getProjectAccesses()
     *
     * @var string<\Platformsh\Client\Model\ProjectAccess[]>
     */
    private static $projectAccessesCache = [];

    /**
     * A cache of not-found environment IDs.
     *
     * @see Api::getEnvironment()
     *
     * @var string[]
     */
    private static $notFound = [];

    /**
     * Session storage, via files or credential helpers.
     *
     * @see Api::initSessionStorage()
     *
     * @var \Platformsh\Client\Session\Storage\SessionStorageInterface|null
     */
    private $sessionStorage;

    /**
     * Sets whether we are currently verifying login using a test request.
     *
     * @var bool
     */
    public $inLoginCheck = false;

    /**
     * Constructor.
     *
     * @param Config|null $config
     * @param CacheProvider|null $cache
     * @param OutputInterface|null $output
     * @param TokenConfig|null $tokenConfig
     * @param EventDispatcherInterface|null $dispatcher
     */
    public function __construct(
        Config $config = null,
        CacheProvider $cache = null,
        OutputInterface $output = null,
        TokenConfig $tokenConfig = null,
        EventDispatcherInterface $dispatcher = null
    ) {
        $this->config = $config ?: new Config();
        $this->output = $output ?: new ConsoleOutput();
        $this->stdErr = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput(): $output;
        $this->tokenConfig = $tokenConfig ?: new TokenConfig($this->config);
        $this->dispatcher = $dispatcher ?: new EventDispatcher();
        $this->cache = $cache ?: CacheFactory::createCacheProvider($this->config);
    }

    /**
     * Returns whether the CLI is authenticating using an API token.
     *
     * @param bool $includeStored
     *
     * @return bool
     */
    public function hasApiToken($includeStored = true)
    {
        return $this->tokenConfig->getAccessToken() || $this->tokenConfig->getApiToken($includeStored);
    }

    /**
     * Lists existing sessions.
     *
     * Excludes API-token-specific session IDs.
     *
     * @return string[]<commit_msg>Fix ConsoleLogger TypeError in Api class due to null stdErr property<commit_after><?php

namespace Platformsh\Cli\Service;

use CommerceGuys\Guzzle\Oauth2\AccessToken;
use Doctrine\Common\Cache\CacheProvider;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Event\ErrorEvent;
use GuzzleHttp\Exception\BadResponseException;
use Platformsh\Cli\CredentialHelper\Manager;
use Platformsh\Cli\CredentialHelper\SessionStorage;
use Platformsh\Cli\Event\EnvironmentsChangedEvent;
use Platformsh\Cli\Model\Route;
use Platformsh\Cli\Util\NestedArrayUtil;
use Platformsh\Client\Connection\Connector;
use Platformsh\Client\Exception\ApiResponseException;
use Platformsh\Client\Model\Deployment\EnvironmentDeployment;
use Platformsh\Client\Model\Environment;
use Platformsh\Client\Model\Organization\Organization;
use Platformsh\Client\Model\Project;
use Platformsh\Client\Model\ProjectAccess;
use Platformsh\Client\Model\ProjectStub;
use Platformsh\Client\Model\Resource as ApiResource;
use Platformsh\Client\Model\SshKey;
use Platformsh\Client\Model\User;
use Platformsh\Client\PlatformClient;
use Platformsh\Client\Session\SessionInterface;
use Platformsh\Client\Session\Storage\File;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 * Decorates the PlatformClient API client to provide aggressive caching.
 */
class Api
{
    /** @var EventDispatcherInterface */
    public $dispatcher;

    /** @var Config */
    private $config;

    /** @var \Doctrine\Common\Cache\CacheProvider */
    private $cache;

    /** @var OutputInterface */
    private $output;

    /** @var OutputInterface */
    private $stdErr;

    /** @var TokenConfig */
    private $tokenConfig;

    /**
     * The library's API client object.
     *
     * This is static so that a freshly logged-in client can then be reused by a parent command with a different service container.
     *
     * @var PlatformClient
     */
    private static $client;

    /**
     * A cache of environments lists, keyed by project ID.
     *
     * @var string<Environment[]>
     */
    private static $environmentsCache = [];

    /**
     * A cache of account details arrays, keyed by project ID.
     *
     * @see Api::getAccount()
     *
     * @var string<array>
     */
    private static $accountsCache = [];

    /**
     * A cache of project access lists, keyed by project ID.
     *
     * @see Api::getProjectAccesses()
     *
     * @var string<\Platformsh\Client\Model\ProjectAccess[]>
     */
    private static $projectAccessesCache = [];

    /**
     * A cache of not-found environment IDs.
     *
     * @see Api::getEnvironment()
     *
     * @var string[]
     */
    private static $notFound = [];

    /**
     * Session storage, via files or credential helpers.
     *
     * @see Api::initSessionStorage()
     *
     * @var \Platformsh\Client\Session\Storage\SessionStorageInterface|null
     */
    private $sessionStorage;

    /**
     * Sets whether we are currently verifying login using a test request.
     *
     * @var bool
     */
    public $inLoginCheck = false;

    /**
     * Constructor.
     *
     * @param Config|null $config
     * @param CacheProvider|null $cache
     * @param OutputInterface|null $output
     * @param TokenConfig|null $tokenConfig
     * @param EventDispatcherInterface|null $dispatcher
     */
    public function __construct(
        Config $config = null,
        CacheProvider $cache = null,
        OutputInterface $output = null,
        TokenConfig $tokenConfig = null,
        EventDispatcherInterface $dispatcher = null
    ) {
        $this->config = $config ?: new Config();
        $this->output = $output ?: new ConsoleOutput();
        $this->stdErr = $this->output instanceof ConsoleOutputInterface ? $this->output->getErrorOutput(): $this->output;
        $this->tokenConfig = $tokenConfig ?: new TokenConfig($this->config);
        $this->dispatcher = $dispatcher ?: new EventDispatcher();
        $this->cache = $cache ?: CacheFactory::createCacheProvider($this->config);
    }

    /**
     * Returns whether the CLI is authenticating using an API token.
     *
     * @param bool $includeStored
     *
     * @return bool
     */
    public function hasApiToken($includeStored = true)
    {
        return $this->tokenConfig->getAccessToken() || $this->tokenConfig->getApiToken($includeStored);
    }

    /**
     * Lists existing sessions.
     *
     * Excludes API-token-specific session IDs.
     *
     * @return string[]
{'fraction_non_alphanumeric': 0.11020967232114635, 'fraction_numerical': 0.00021072595090085343, 'mean_word_length': 3.1341463414634148, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 38, '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': '1262164', 'n_tokens_mistral': 2696, 'n_tokens_neox': 2628, 'n_words': 637}