QuoteCrawler::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chemaclass\StockTicker\Domain\Crawler;
6
7
use Chemaclass\StockTicker\Domain\Crawler\Mapper\CrawledInfoMapperInterface;
8
use Chemaclass\StockTicker\Domain\ReadModel\Site;
9
use Chemaclass\StockTicker\Domain\ReadModel\Symbol;
10
use Symfony\Contracts\HttpClient\HttpClientInterface;
11
12
final class QuoteCrawler implements QuoteCrawlerInterface
13
{
14
    private HttpClientInterface $httpClient;
15
16
    private CrawledInfoMapperInterface $crawledInfoMapper;
17
18
    /** @var SiteCrawlerInterface[] */
19
    private array $siteCrawlers;
20
21 6
    public function __construct(
22
        HttpClientInterface $httpClient,
23
        CrawledInfoMapperInterface $crawledInfoMapper,
24
        SiteCrawlerInterface ...$siteCrawlers,
25
    ) {
26 6
        $this->httpClient = $httpClient;
27 6
        $this->crawledInfoMapper = $crawledInfoMapper;
28 6
        $this->siteCrawlers = $siteCrawlers;
29 6
    }
30
31 6
    public function crawlStock(string ...$symbolStrings): CrawlResult
32
    {
33 6
        $result = [];
34 6
        $symbols = $this->mapSymbols(...$symbolStrings);
35
36 6
        foreach ($symbols as $symbol) {
37 5
            $sites = $this->crawlAllSitesForSymbol($symbol);
38 5
            $info = $this->flat(...$sites);
39
40 5
            $quote = $this->crawledInfoMapper->mapQuote($info);
41
42 5
            if ($quote->hasMandatoryFields()) {
43 3
                $result[$symbol->toString()] = $quote;
44
            }
45
        }
46
47 6
        return new CrawlResult($result);
48
    }
49
50
    /**
51
     * @return Symbol[]
52
     */
53 6
    private function mapSymbols(string ...$symbolStrings): array
54
    {
55 6
        return array_map(
56 6
            static fn (string $symbol) => Symbol::fromString($symbol),
57
            $symbolStrings,
58
        );
59
    }
60
61
    /**
62
     * @return Site[]
63
     */
64 5
    private function crawlAllSitesForSymbol(Symbol $symbol): array
65
    {
66 5
        return array_map(
67 5
            fn (SiteCrawlerInterface $crawler): Site => $crawler->crawl($this->httpClient, $symbol),
68 5
            $this->siteCrawlers,
69
        );
70
    }
71
72
    /**
73
     * Combine all sites keys values, merging the values from the shared keys in the same array.
74
     */
75 5
    private function flat(Site ...$sites): array
76
    {
77 5
        return array_merge_recursive(...$this->normalizeSites(...$sites));
78
    }
79
80
    /**
81
     * @return list<array<array-key, mixed>>
0 ignored issues
show
Bug introduced by
The type Chemaclass\StockTicker\Domain\Crawler\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
82
     */
83 5
    private function normalizeSites(Site ...$sites): array
84
    {
85 5
        return array_map(
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_map(functio..., array_values($sites)) returns the type array which is incompatible with the documented return type Chemaclass\StockTicker\Domain\Crawler\list.
Loading history...
86 5
            static fn (Site $site): array => $site->crawled(),
87 5
            array_values($sites),
88
        );
89
    }
90
}
91