MarketWatchSiteCrawler::crawl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
rs 9.9666
ccs 9
cts 9
cp 1
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chemaclass\StockTicker\Domain\Crawler\Site\MarketWatch;
6
7
use Chemaclass\StockTicker\Domain\Crawler\SiteCrawlerInterface;
8
use Chemaclass\StockTicker\Domain\ReadModel\Site;
9
use Chemaclass\StockTicker\Domain\ReadModel\Symbol;
10
use Symfony\Contracts\HttpClient\HttpClientInterface;
11
12
final class MarketWatchSiteCrawler implements SiteCrawlerInterface
13
{
14
    private const REQUEST_METHOD = 'GET';
15
16
    private const REQUEST_URL = 'https://www.marketwatch.com/investing/stock/%s';
17
18
    /** @var HtmlCrawlerInterface[] */
19
    private array $crawlers;
20
21 4
    public function __construct(array $crawlers)
22
    {
23 4
        $this->crawlers = $crawlers;
24 4
    }
25
26 4
    public function crawl(HttpClientInterface $httpClient, Symbol $symbol): Site
27
    {
28 4
        $symbol = mb_strtolower($symbol->toString());
29 4
        $url = sprintf(self::REQUEST_URL, $symbol);
30
31
        $html = $httpClient
32 4
            ->request(self::REQUEST_METHOD, $url)
33 4
            ->getContent($throw = false);
34
35 4
        $crawled = [];
36
37 4
        foreach ($this->crawlers as $name => $crawler) {
38 4
            $crawled[$name] = $crawler->crawlHtml($html);
39
        }
40
41 4
        return new Site($crawled);
42
    }
43
}
44