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 | 19 | public function __construct( |
|
31 | SearchTermInterface $searchTerm, GoogleProxyInterface $proxy = null, |
||
32 | string $googleDomain = 'google.com', string $countryCode = '' |
||
33 | ) { |
||
34 | 19 | $this->proxy = is_null($proxy) ? new NoProxy() : $proxy; |
|
|
|||
35 | 19 | $this->searchTerm = $searchTerm; |
|
36 | |||
37 | 19 | if (mb_stripos($googleDomain, 'google.') === false || mb_stripos($googleDomain, 'http') === 0) { |
|
38 | 2 | throw new \InvalidArgumentException('Invalid google domain'); |
|
39 | } |
||
40 | 17 | $this->googleDomain = $googleDomain; |
|
41 | |||
42 | 17 | $this->countryCode = mb_strtoupper($countryCode); |
|
43 | 17 | } |
|
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 | 14 | public function getResults(): ResultList |
|
77 | |||
78 | /** |
||
79 | * If $resultLink is a valid link, this method assembles the Result and adds it to $googleResults |
||
80 | * |
||
81 | * @param Link $resultLink |
||
82 | * @param DOMElement $descriptionElement |
||
83 | * @return Result |
||
84 | * @throws InvalidResultException |
||
85 | */ |
||
86 | 13 | private function parseResult(Link $resultLink, DOMElement $descriptionElement): Result |
|
99 | |||
100 | /** |
||
101 | * Parses the URL using the parser provided by $proxy |
||
102 | * |
||
103 | * @param string $url |
||
104 | * @return string |
||
105 | * @throws InvalidResultException |
||
106 | */ |
||
107 | 13 | private function parseUrl(string $url): string |
|
111 | |||
112 | /** |
||
113 | * Assembles the Google URL using the previously informed data |
||
114 | */ |
||
115 | 17 | private function getGoogleUrl(): string |
|
125 | } |
||
126 |
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..