1 | <?php declare(strict_types=1); |
||
25 | class Client implements ClientInterface |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * Default user agent provided with the package |
||
30 | */ |
||
31 | const DEFAULT_USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1'; |
||
32 | |||
33 | /** |
||
34 | * @var \GuzzleHttp\ClientInterface |
||
35 | */ |
||
36 | protected $guzzleClient; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $userAgent; |
||
42 | |||
43 | /** |
||
44 | * @param \GuzzleHttp\ClientInterface $guzzleClient |
||
45 | * @param string $userAgent |
||
46 | */ |
||
47 | 11 | public function __construct(\GuzzleHttp\ClientInterface $guzzleClient, string $userAgent = self::DEFAULT_USER_AGENT) |
|
52 | |||
53 | /** |
||
54 | * @param string $userAgent The new user-agent |
||
55 | * @return Client |
||
56 | */ |
||
57 | public function setUserAgent(string $userAgent) : Client |
||
58 | { |
||
59 | $this->userAgent = $userAgent; |
||
60 | |||
61 | return $this; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param string $url |
||
66 | * @param \DateTime $modifiedSince |
||
67 | * @return ResponseInterface |
||
68 | * @throws \GuzzleHttp\Exception\GuzzleException |
||
69 | */ |
||
70 | 3 | public function getResponse(string $url, \DateTime $modifiedSince) : ResponseInterface |
|
71 | { |
||
72 | try { |
||
73 | 3 | $options = $this->getOptions($modifiedSince); |
|
74 | |||
75 | 3 | return new Response($this->guzzleClient->request('get', $url, $options)); |
|
76 | 2 | } catch (BadResponseException $e) { |
|
77 | 2 | switch ((int) $e->getResponse()->getStatusCode()) { |
|
78 | 2 | case 404: |
|
79 | 1 | throw new NotFoundException($e->getMessage()); |
|
80 | default: |
||
81 | 1 | throw new ServerErrorException($e->getMessage()); |
|
82 | } |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param iterable $requests |
||
88 | * @param ReaderInterface $reader |
||
89 | * @return \Generator |
||
90 | */ |
||
91 | 4 | public function getPromises(iterable $requests, ReaderInterface $reader) : \Generator |
|
97 | |||
98 | /** |
||
99 | * @param Request $request |
||
100 | * @param ReaderInterface $reader |
||
101 | * @return PromiseInterface |
||
102 | */ |
||
103 | 4 | protected function getPromise(Request $request, ReaderInterface $reader) : PromiseInterface |
|
104 | { |
||
105 | 4 | $promise = $this->newPromise($request); |
|
106 | |||
107 | $promise->then(function ($psrResponse) use ($request, $reader) { |
||
108 | try { |
||
109 | 3 | $request->setResponse(new Response($psrResponse)); |
|
110 | 3 | $reader->handle($request); |
|
111 | 1 | } catch (\Exception $e) { |
|
112 | 1 | $reader->handleError($request, $e); |
|
113 | } |
||
114 | }, function ($error) use ($request, $reader) { |
||
115 | 1 | $reader->handleError($request, $error); |
|
116 | 4 | }); |
|
117 | |||
118 | 4 | return $promise; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param Request $request |
||
123 | * @return PromiseInterface |
||
124 | */ |
||
125 | 4 | protected function newPromise(Request $request) : PromiseInterface |
|
131 | |||
132 | /** |
||
133 | * @param \DateTime $modifiedSince |
||
134 | * @return array |
||
135 | */ |
||
136 | 7 | protected function getOptions(\DateTime $modifiedSince) : array |
|
145 | } |
||
146 |