1 | <?php |
||
19 | class Crawler |
||
20 | { |
||
21 | /** @var GoogleProxyInterface $proxy */ |
||
22 | protected $proxy; |
||
23 | /** @var SearchTermInterface $searchTerm */ |
||
24 | private $searchTerm; |
||
25 | /** @var string $countrySpecificSuffix */ |
||
26 | private $googleDomain; |
||
27 | /** @var string $countryCode */ |
||
28 | private $countryCode; |
||
29 | |||
30 | 11 | public function __construct( |
|
31 | SearchTermInterface $searchTerm, GoogleProxyInterface $proxy = null, |
||
32 | string $googleDomain = 'google.com', string $countryCode = '' |
||
33 | ) { |
||
34 | 11 | $this->proxy = is_null($proxy) ? new NoProxy() : $proxy; |
|
|
|||
35 | 11 | $this->searchTerm = $searchTerm; |
|
36 | |||
37 | 11 | if (mb_stripos($googleDomain, 'google.') === false || mb_stripos($googleDomain, 'http') === 0) { |
|
38 | 2 | throw new \InvalidArgumentException('Invalid google domain'); |
|
39 | } |
||
40 | 9 | $this->googleDomain = $googleDomain; |
|
41 | |||
42 | 9 | $this->countryCode = mb_strtoupper($countryCode); |
|
43 | 9 | } |
|
44 | |||
45 | /** |
||
46 | * Returns the 100 first found results for the specified search term |
||
47 | * |
||
48 | * @return ResultList |
||
49 | * @throws \GuzzleHttp\Exception\ServerException If the proxy was overused |
||
50 | * @throws \GuzzleHttp\Exception\ConnectException If the proxy is unavailable or $countrySpecificSuffix is invalid |
||
51 | */ |
||
52 | 6 | public function getResults(): ResultList |
|
81 | |||
82 | /** |
||
83 | * If $resultLink is a valid link, this method assembles the Result and adds it to $googleResults |
||
84 | * |
||
85 | * @param Link $resultLink |
||
86 | * @param DOMElement $descriptionElement |
||
87 | * @return Result |
||
88 | * @throws InvalidResultException |
||
89 | */ |
||
90 | 5 | private function parseResult(Link $resultLink, DOMElement $descriptionElement): Result |
|
103 | |||
104 | /** |
||
105 | * Parses the URL using the parser provided by $proxy |
||
106 | * |
||
107 | * @param string $url |
||
108 | * @return string |
||
109 | * @throws InvalidResultException |
||
110 | */ |
||
111 | 5 | private function parseUrl(string $url): string |
|
115 | |||
116 | /** |
||
117 | * Assembles the Google URL using the previously informed data |
||
118 | */ |
||
119 | 9 | private function getGoogleUrl(): string |
|
120 | { |
||
121 | 9 | $domain = $this->googleDomain; |
|
122 | 9 | $url = "https://$domain/search?q={$this->searchTerm}&num=100"; |
|
123 | 9 | if (!empty($this->countryCode)) { |
|
124 | 2 | $url .= "&gl={$this->countryCode}"; |
|
125 | } |
||
126 | |||
127 | 9 | return $url; |
|
128 | } |
||
129 | |||
130 | 5 | private function isImageSuggestion(DomCrawler $resultCrawler) |
|
138 | } |
||
139 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..