ApiProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 74
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getAPIResponse() 0 17 3
A getBatchAPIResponse() 0 17 3
A createRequests() 0 9 2
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