Total Complexity | 4 |
Total Lines | 39 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
12 | final class BarronsSiteCrawler implements SiteCrawlerInterface |
||
13 | { |
||
14 | private const REQUEST_METHOD = 'GET'; |
||
15 | |||
16 | private const REQUEST_URL = 'https://www.barrons.com/quote/stock/%s'; |
||
17 | |||
18 | private const EXAMPLE_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'; |
||
19 | |||
20 | /** @var HtmlCrawlerInterface[] */ |
||
21 | private array $crawlers; |
||
22 | |||
23 | 4 | public function __construct(array $crawlers) |
|
24 | { |
||
25 | 4 | $this->crawlers = $crawlers; |
|
26 | 4 | } |
|
27 | |||
28 | 4 | public function crawl(HttpClientInterface $httpClient, Symbol $symbol): Site |
|
29 | { |
||
30 | 4 | $symbol = mb_strtolower($symbol->toString()); |
|
31 | 4 | $url = sprintf(self::REQUEST_URL, $symbol); |
|
32 | |||
33 | $html = $httpClient |
||
34 | 4 | ->request(self::REQUEST_METHOD, $url, $this->buildRequestHeaders()) |
|
35 | 4 | ->getContent($throw = false); |
|
36 | |||
37 | 4 | $crawled = []; |
|
38 | |||
39 | 4 | foreach ($this->crawlers as $name => $crawler) { |
|
40 | 4 | $crawled[$name] = $crawler->crawlHtml($html); |
|
41 | } |
||
42 | |||
43 | 4 | return new Site($crawled); |
|
44 | } |
||
45 | |||
46 | 4 | private function buildRequestHeaders(): array |
|
51 | ], |
||
52 | ]; |
||
53 | } |
||
55 |