mirror of
https://github.com/doctrine/doctrine-website.git
synced 2026-03-23 22:32:11 +01:00
To prevent mocking GitHub Client's magic methods, the actual instance will now be injected
51 lines
1.1 KiB
PHP
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;
|
|
}
|
|
}
|