Files
archived-doctrine-website/lib/Github/GithubClientProvider.php
Claudio Zizza 0b8cfa719a Directly inject GitHub Repo instance as service
To prevent mocking GitHub Client's magic methods, the actual instance will now be injected
2024-02-06 21:01:50 +01:00

51 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Doctrine\Website\Github;
use Github\Api\Repo;
use Github\AuthMethod;
use Github\Client;
use InvalidArgumentException;
use Psr\Cache\CacheItemPoolInterface;
use function assert;
final class GithubClientProvider
{
private bool $authenticated = false;
public function __construct(
private readonly Client $githubClient,
CacheItemPoolInterface $cache,
private readonly string $githubHttpToken,
) {
if ($githubHttpToken === '') {
throw new InvalidArgumentException('You must configure a Github http token.');
}
$this->githubClient->addCache($cache);
}
public function repositories(): Repo
{
$repositories = $this->getGithubClient()->api('repo');
assert($repositories instanceof Repo);
return $repositories;
}
private function getGithubClient(): Client
{
if ($this->authenticated === false) {
$this->githubClient->authenticate($this->githubHttpToken, '', AuthMethod::ACCESS_TOKEN);
$this->authenticated = true;
}
return $this->githubClient;
}
}