Completed
Push — master ( b86906...5e7e78 )
by Alex
03:01
created

Client::getPromise()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 11
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\Adapter\Guzzle;
12
13
use FeedIo\Adapter\ClientInterface;
14
use FeedIo\Adapter\Guzzle\Async\ReaderInterface;
15
use FeedIo\Adapter\NotFoundException;
16
use FeedIo\Adapter\ResponseInterface;
17
use FeedIo\Adapter\ServerErrorException;
18
use FeedIo\Async\Request;
19
use \GuzzleHttp\Promise\PromiseInterface;
20
use GuzzleHttp\Exception\BadResponseException;
21
22
/**
23
 * Guzzle dependent HTTP client
24
 */
25
class Client implements ClientInterface
26
{
27
    /**
28
     * @var \GuzzleHttp\ClientInterface
29
     */
30
    protected $guzzleClient;
31
32
    /**
33
     * @param \GuzzleHttp\ClientInterface $guzzleClient
34
     */
35 11
    public function __construct(\GuzzleHttp\ClientInterface $guzzleClient)
36
    {
37 11
        $this->guzzleClient = $guzzleClient;
38 11
    }
39
40
    /**
41
     * @param string $url
42
     * @param \DateTime $modifiedSince
43
     * @return ResponseInterface
44
     * @throws \GuzzleHttp\Exception\GuzzleException
45
     */
46 3
    public function getResponse(string $url, \DateTime $modifiedSince) : ResponseInterface
47
    {
48
        try {
49 3
            $options = $this->getOptions($modifiedSince);
50
51 3
            return new Response($this->guzzleClient->request('get', $url, $options));
52 2
        } catch (BadResponseException $e) {
53 2
            switch ((int) $e->getResponse()->getStatusCode()) {
54 2
                case 404:
55 1
                    throw new NotFoundException($e->getMessage());
56
                default:
57 1
                    throw new ServerErrorException($e->getMessage());
58
            }
59
        }
60
    }
61
62
    /**
63
     * @param iterable $requests
64
     * @param ReaderInterface $reader
65
     * @return \Generator
66
     */
67 4
    public function getPromises(iterable $requests, ReaderInterface $reader) : \Generator
68
    {
69 4
        foreach ($requests as $request) {
70 4
            yield $this->getPromise($request, $reader);
71
        }
72 4
    }
73
74
    /**
75
     * @param Request $request
76
     * @param ReaderInterface $reader
77
     * @return PromiseInterface
78
     */
79 4
    protected function getPromise(Request $request, ReaderInterface $reader) : PromiseInterface
80
    {
81 4
        $promise = $this->newPromise($request);
82
83 3
        $promise->then(function ($psrResponse) use ($request, $reader) {
84
            try {
85 3
                $request->setResponse(new Response($psrResponse));
86 3
                $reader->handle($request);
87 1
            } catch (\Exception $e) {
88 1
                $reader->handleError($request, $e);
89
            }
90
        }, function ($error) use ($request, $reader) {
91 1
            $reader->handleError($request, $error);
92 4
        });
93
94 4
        return $promise;
95
    }
96
97
    /**
98
     * @param Request $request
99
     * @return PromiseInterface
100
     */
101 4
    protected function newPromise(Request $request) : PromiseInterface
102
    {
103 4
        $options = $this->getOptions($request->getModifiedSince());
104
105 4
        return $this->guzzleClient->requestAsync('GET', $request->getUrl(), $options);
106
    }
107
108
    /**
109
     * @param \DateTime $modifiedSince
110
     * @return array
111
     */
112 7
    protected function getOptions(\DateTime $modifiedSince) : array
113
    {
114
        return [
115
            'headers' => [
116 7
                'User-Agent' => 'Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1',
117 7
                'If-Modified-Since' => $modifiedSince->format(\DateTime::RFC2822)
118
            ]
119
        ];
120
    }
121
}
122