Completed
Push — issue/127 ( 820451 )
by Alex
03:52
created

Client::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Async;
12
13
use \GuzzleHttp\ClientInterface;
14
use \GuzzleHttp\Promise\PromiseInterface;
15
use \FeedIo\Adapter\Guzzle\Response;
16
17
class Client
18
{
19
20
    /**
21
     * @var \GuzzleHttp\ClientInterface
22
     */
23
    protected $guzzleClient;
24
25
    /**
26
     * @var Reader
27
     */
28
    protected $reader;
29
30
    /**
31
     * Client constructor.
32
     * @param ClientInterface $guzzleClient
33
     * @param Reader $reader
34
     */
35
    public function __construct(ClientInterface $guzzleClient, Reader $reader)
36
    {
37
        $this->guzzleClient = $guzzleClient;
38
        $this->reader = $reader;
39
    }
40
41
42
    /**
43
     * @param iterable $requests
44
     * @return \Generator
45
     */
46
    public function getPromises(iterable $requests) : \Generator
47
    {
48
        foreach ($requests as $request) {
49
            yield $this->getPromise($request);
50
        }
51
    }
52
53
    /**
54
     * @param Request $request
55
     * @return PromiseInterface
56
     */
57
    protected function getPromise(Request $request) : PromiseInterface
58
    {
59
        $options = [
60
            'headers' => [
61
                'User-Agent' => 'Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1',
62
                'If-Modified-Since' => $request->getModifiedSince()->format(\DateTime::RFC2822)
63
            ]
64
        ];
65
        $promise = $this->guzzleClient->requestAsync('GET', $request->getUrl(), $options);
66
67
        $reader= $this->reader;
68
        $promise->then(function ($psrResponse) use ($request, $reader) {
69
            $request->setResponse(new Response($psrResponse));
70
            $reader->handle($request);
71
        });
72
73
        return $promise;
74
    }
75
}
76