Просмотр файла app/Services/GithubService.php

Размер файла: 1.64Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Services;
  6.  
  7. use GuzzleHttp\Client;
  8. use Shieldon\SimpleCache\Cache;
  9. use Throwable;
  10.  
  11. class GithubService
  12. {
  13. private Client $client;
  14.  
  15. public function __construct(private Cache $cache)
  16. {
  17. $this->client = new Client([
  18. 'base_uri' => 'https://api.github.com/repos/visavi/motor/',
  19. 'timeout' => 3.0,
  20. ]);
  21. }
  22.  
  23. /**
  24. * Send request
  25. *
  26. * @param string $uri
  27. *
  28. * @return array
  29. */
  30. public function sendRequest(string $uri): array
  31. {
  32. try {
  33. $content = $this->cache->get($uri);
  34. if ($content) {
  35. return json_decode($content, true);
  36. }
  37.  
  38. $params = [
  39. 'query' => [
  40. 'per_page' => 10,
  41. ]
  42. ];
  43.  
  44. $response = $this->client->get($uri, $params);
  45. $content = $response->getBody()->getContents();
  46.  
  47. if ($content) {
  48. $this->cache->set($uri, $content, 3600);
  49.  
  50. return json_decode($content, true);
  51. }
  52. } catch (Throwable $e) {
  53. // nothing
  54. }
  55.  
  56. return [];
  57. }
  58.  
  59. /**
  60. * Get releases
  61. *
  62. * @return array
  63. */
  64. public function getReleases(): array
  65. {
  66. return $this->sendRequest('releases');
  67. }
  68.  
  69. /**
  70. * Get last release
  71. *
  72. * @return array
  73. */
  74. public function getLastRelease(): array
  75. {
  76. $releases = $this->sendRequest('releases');
  77.  
  78. return $releases[0] ?? [];
  79. }
  80.  
  81. public function getCommits(): array
  82. {
  83. return $this->sendRequest('commits');
  84. }
  85. }