|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the wow-apps/symfony-packagist project |
|
4
|
|
|
* https://github.com/wow-apps/symfony-packagist |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2017 WoW-Apps |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WowApps\PackagistBundle\Service; |
|
13
|
|
|
|
|
14
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
15
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
16
|
|
|
use GuzzleHttp\Pool; |
|
17
|
|
|
use GuzzleHttp\Psr7\Request; |
|
18
|
|
|
use GuzzleHttp\Psr7\Response; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class ApiProvider |
|
22
|
|
|
* |
|
23
|
|
|
* @author Alexey Samara <[email protected]> |
|
24
|
|
|
* @package wow-apps/symfony-packagist |
|
25
|
|
|
*/ |
|
26
|
|
|
class ApiProvider |
|
27
|
|
|
{ |
|
28
|
|
|
const POOL_CONCURRENCY = 25; |
|
29
|
|
|
|
|
30
|
|
|
/** @var GuzzleClient */ |
|
31
|
|
|
private $guzzleClient; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Packagist constructor. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct() { |
|
37
|
|
|
$this->guzzleClient = new GuzzleClient(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $url |
|
42
|
|
|
* @return array |
|
43
|
|
|
* @throws \RuntimeException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getAPIResponse(string $url): array |
|
46
|
|
|
{ |
|
47
|
|
|
try { |
|
48
|
|
|
$request = $this->guzzleClient->get($url); |
|
49
|
|
|
} catch (ClientException $e) { |
|
50
|
|
|
throw new \RuntimeException($e->getMessage()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$response = $request->getBody(); |
|
54
|
|
|
$json = json_decode($response, true); |
|
55
|
|
|
|
|
56
|
|
|
if (!$json) { |
|
57
|
|
|
throw new \RuntimeException(sprintf('Can\'t parse json. [url: %s]', $url)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $json; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $urls |
|
65
|
|
|
* @param int $concurrency |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getBatchAPIResponse(array $urls, int $concurrency = self::POOL_CONCURRENCY): array |
|
69
|
|
|
{ |
|
70
|
|
|
$requests = $this->createRequests($urls); |
|
71
|
|
|
$responses = Pool::batch($this->guzzleClient, $requests, ['concurrency' => $concurrency]); |
|
72
|
|
|
|
|
73
|
|
|
/** @var Response $response */ |
|
74
|
|
|
foreach ($responses as $key => $response) { |
|
75
|
|
|
$json = json_decode($response->getBody(), true); |
|
76
|
|
|
if (!$json) { |
|
77
|
|
|
continue; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$responses[$key] = $json; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $responses; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param array $links |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
private function createRequests(array $links): array |
|
91
|
|
|
{ |
|
92
|
|
|
$requests = []; |
|
93
|
|
|
foreach ($links as $link) { |
|
94
|
|
|
$requests[] = new Request('GET', $link); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $requests; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|