Completed
Push — master ( 35f9c1...7f1afc )
by Xavier
01:04
created

ApiDataFetcher::fetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
ccs 8
cts 8
cp 1
cc 1
eloc 9
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor;
4
5
use Generator;
6
use GuzzleHttp\Promise\EachPromise;
7
use Psr\Http\Message\ResponseInterface;
8
use GuzzleHttp\Exception\RequestException;
9
use GrahamCampbell\GuzzleFactory\GuzzleFactory;
10
use PubPeerFoundation\PublicationDataExtractor\Resources\Resource;
11
use PubPeerFoundation\PublicationDataExtractor\Helpers\ClassHelper;
12
use PubPeerFoundation\PublicationDataExtractor\Identifiers\Identifier;
13
14
class ApiDataFetcher
15
{
16
    protected $identifier;
17
18
    protected $client;
19
20
    protected $resources = [];
21
22
    public $apiData;
23 3
24
    protected $errors = [];
25 3
26 3
    public function __construct(Identifier $identifier)
27 3
    {
28
        $this->identifier = $identifier;
29
        $this->client = GuzzleFactory::make(compact('headers'), 100);
30
    }
31
32 3
    /**
33
     * Fetch data from API calls promises.
34 3
     */
35 3
    public function fetch(): void
36 3
    {
37 3
        (new EachPromise($this->getPromises(), [
38 3
            'concurrency' => 3,
39 3
            'fulfilled' => function (ResponseInterface $response, $index) {
40 3
                $this->apiData[] = $this->getResourceAtIndex($index)
41 3
                    ->getDataFrom((string) $response->getBody());
42 3
            },
43
            'rejected' => function (RequestException $exception, $index) {
44
                $resourceName = ClassHelper::get_class_name($this->getResourceAtIndex($index));
45
                $this->errors[$resourceName] = $exception->getCode();
46
            },
47
        ]))->promise()->wait();
48
    }
49 3
50
    /**
51 3
     * Get list of API calls promises.
52 3
     *
53
     * @return Generator
54 3
     */
55 3
    protected function getPromises(): Generator
56 3
    {
57 3
        foreach ($this->identifier->getRelatedResources() as $resourceClass) {
58
            $resource = $this->instantiateResource($resourceClass);
59
60 3
            $promise = $this->client->requestAsync(
61
                'GET',
62 3
                $resource->getApiUrl(),
63
                $resource->getRequestOptions()
64
            );
65
66
            yield $promise;
67
        }
68
    }
69
70
    public function getData(): array
71 3
    {
72
        return array_values(
73 3
            array_filter($this->apiData)
74
        );
75
    }
76
77
    public function getErrors(): array
78
    {
79
        return $this->errors;
80
    }
81
82
    /**
83 3
     * Get resource instance from resources array.
84
     *
85 3
     * @param int $index
86
     *
87
     * @return resource
88
     */
89
    protected function getResourceAtIndex(int $index): Resource
90
    {
91
        return $this->resources[$index];
92
    }
93
94
    /**
95
     * Create and store an instance of the Resource class.
96
     *
97
     * @param string $resourceClass
98
     *
99
     * @return resource
100
     */
101
    protected function instantiateResource($resourceClass): Resource
102
    {
103
        return $this->resources[] = new $resourceClass($this->identifier);
104
    }
105
}
106