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 | 17 | public function __construct( |
|
46 | |||
47 | /** |
||
48 | * Returns the 100 first found results for the specified search term |
||
49 | * |
||
50 | * @return ResultList |
||
51 | * @throws \GuzzleHttp\Exception\ServerException If the proxy was overused |
||
52 | * @throws \GuzzleHttp\Exception\ConnectException If the proxy is unavailable or $countrySpecificSuffix is invalid |
||
53 | */ |
||
54 | 12 | public function getResults(): ResultList |
|
55 | { |
||
56 | 12 | $googleUrl = $this->getGoogleUrl(); |
|
57 | 12 | $response = $this->proxy->getHttpResponse($googleUrl); |
|
58 | 11 | $stringResponse = (string) $response->getBody(); |
|
59 | 11 | $domCrawler = new DomCrawler($stringResponse); |
|
60 | 11 | $googleResultList = $domCrawler->filterXPath('//div[@class="g" and h3[@class="r" and a]]'); |
|
61 | 11 | if ($googleResultList->count() === 0) { |
|
62 | 1 | throw new InvalidGoogleHtmlException('No parseable element found'); |
|
63 | } |
||
64 | |||
65 | 10 | $resultList = new ResultList($googleResultList->count()); |
|
66 | |||
67 | 10 | foreach ($googleResultList as $googleResultElement) { |
|
68 | try { |
||
69 | 10 | $parsedResult = $this->parseDomElement($googleResultElement); |
|
70 | 10 | $resultList->addResult($parsedResult); |
|
71 | 10 | } catch (InvalidResultException $exception) { |
|
72 | 10 | error_log( |
|
73 | 10 | 'Error parsing the following result: ' . print_r($googleResultElement, true), |
|
74 | 10 | 3, |
|
75 | 10 | __DIR__ . '/../var/log/crawler-errors.log' |
|
76 | ); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | 10 | return $resultList; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * If $resultLink is a valid link, this method assembles the Result and adds it to $googleResults |
||
85 | * |
||
86 | * @param Link $resultLink |
||
87 | * @param DOMElement $descriptionElement |
||
88 | * @return Result |
||
89 | * @throws InvalidResultException |
||
90 | */ |
||
91 | 10 | private function createResult(Link $resultLink, DOMElement $descriptionElement): Result |
|
92 | { |
||
93 | 10 | $description = $descriptionElement->nodeValue |
|
94 | 10 | ?? 'A description for this result isn\'t available due to the robots.txt file.'; |
|
95 | |||
96 | 10 | $googleResult = new Result(); |
|
97 | $googleResult |
||
98 | 10 | ->setTitle($resultLink->getNode()->nodeValue) |
|
99 | 10 | ->setUrl($this->parseUrl($resultLink->getUri())) |
|
100 | 10 | ->setDescription($description); |
|
101 | |||
102 | 10 | return $googleResult; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * Parses the URL using the parser provided by $proxy |
||
107 | * |
||
108 | * @param string $url |
||
109 | * @return string |
||
110 | * @throws InvalidResultException |
||
111 | */ |
||
112 | 10 | private function parseUrl(string $url): string |
|
113 | { |
||
114 | 10 | return $this->proxy->parseUrl($url); |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Assembles the Google URL using the previously informed data |
||
119 | */ |
||
120 | 15 | private function getGoogleUrl(): string |
|
130 | |||
131 | 10 | private function isImageSuggestion(DomCrawler $resultCrawler) |
|
132 | { |
||
133 | $resultCount = $resultCrawler |
||
139 | |||
140 | 10 | private function parseDomElement(DOMElement $result): Result |
|
162 | } |
||
163 |
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..