Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

codelabmw/larakits

23 stars
3 code files
View codelabmw/larakits on GitHub

app/Services/Github/Github.php

Open in GitHub
use App\Exceptions\ConnectionException;
use Exception;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
use InvalidArgumentException;
 
final readonly class Github
{
public function __construct(
private string $baseUrl = 'https://api.github.com',
) {
//
}
 
public static function ownerAndRepo(string $url): array
{
 
preg_match('/^https?:\/\/github\.com\/([^\/]+)\/([^\/]+)\/?$/', $url, $matches);
 
if (count($matches) !== 3) {
throw new InvalidArgumentException('Invalid repository URL');
}
 
return [$matches[1], $matches[2]];
}
 
public function contents(string $owner, string $repo, string $path): string
{
try {
$response = Http::retry(config('services.github.retry'), fn (int $attempt, Exception $exception): int => $attempt * 1000, fn (Exception $exception, PendingRequest $request): bool => ! ($exception instanceof RequestException && in_array($exception->response->status(), [404, 403])))->withHeader('Accept', 'application/vnd.github.v3+json')->get(
url: "{$this->baseUrl}/repos/{$owner}/{$repo}/contents/{$path}",
);
} catch (RequestException $exception) {
throw new ConnectionException($exception->response, 'Failed to retrieve file contents');
}
 
return $response->json()['content'];
}
 
public function stars(string $owner, string $repo, ?string $token = null): int
{
try {
$request = Http::retry(config('services.github.retry'), fn (int $attempt, Exception $exception): int => $attempt * 1000, fn (Exception $exception, PendingRequest $request): bool => ! ($exception instanceof RequestException && in_array($exception->response->status(), [404, 403, 401])));
 
if ($token !== null && $token !== '' && $token !== '0') {
$request->withHeader('Authorization', "token {$token}");
}
 
$response = $request->get("{$this->baseUrl}/repos/{$owner}/{$repo}");
} catch (RequestException $exception) {
throw new ConnectionException($exception->response, 'Failed to retrieve repository stars');
}
 
return $response->json()['stargazers_count'];
}
}

app/Providers/AppServiceProvider.php

Open in GitHub
use App\Services\Github\Github;
 
class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
// ...
 
$this->app->bind(Github::class, fn (): Github => new Github());
}
 
// ...
}

app/Http/Middleware/HandleGithubStarsCache.php

Open in GitHub
use App\Services\Github\Github;
use Closure;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Symfony\Component\HttpFoundation\Response;
 
class HandleGithubStarsCache
{
public function handle(Request $request, Closure $next): Response
{
$this->cacheGithubStars(App::make(Github::class));
 
return $next($request);
}
 
private function cacheGithubStars(Github $github): void
{
if (Cache::has('github-stars') && Cache::get('github-stars') !== null) {
return;
}
 
Cache::remember('github-stars', 3600, fn (): ?int => $this->getGithubStars($github));
}
 
private function getGithubStars(Github $github): ?int
{
$stars = null;
 
try {
$stars = $github->stars('codelabmw', 'larakits');
} catch (Exception) {
//
}
 
return $stars;
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.