diff --git a/src/Exception/LayoutMetricsFailed.php b/src/Exception/LayoutMetricsFailed.php deleted file mode 100644 index ba3e5242..00000000 --- a/src/Exception/LayoutMetricsFailed.php +++ /dev/null @@ -1,11 +0,0 @@ -responseReader = $responseReader; - } - - /** - * @return $this - * @throws LayoutMetricsFailed - * @throws \HeadlessChromium\Exception\NoResponseAvailable - * @throws \HeadlessChromium\Exception\OperationTimedOut - */ - public function await() - { - $this->response = $this->responseReader->waitForResponse(); - - if (!$this->response->isSuccessful()) { - throw new LayoutMetricsFailed('Could not retrieve layout metrics of the page.'); - } - - return $this; - } - /** * Returns raw page metrics data * @return array - * @throws LayoutMetricsFailed + * @throws CommunicationException\ResponseHasError + * @throws \HeadlessChromium\Exception\NoResponseAvailable + * @throws \HeadlessChromium\Exception\OperationTimedOut */ public function getMetrics(): array { - $response = $this->responseReader->waitForResponse(); + $response = $this->awaitResponse(); return $response->getData()['results']; } /** * Returns size of scrollable area * @return array - * @throws LayoutMetricsFailed + * @throws CommunicationException\ResponseHasError + * @throws \HeadlessChromium\Exception\NoResponseAvailable + * @throws \HeadlessChromium\Exception\OperationTimedOut */ public function getContentSize(): array { - $response = $this->responseReader->waitForResponse(); + $response = $this->awaitResponse(); return $response->getResultData('contentSize'); } /** * Returns metrics relating to the layout viewport * @return array - * @throws LayoutMetricsFailed + * @throws CommunicationException\ResponseHasError + * @throws \HeadlessChromium\Exception\NoResponseAvailable + * @throws \HeadlessChromium\Exception\OperationTimedOut */ public function getLayoutViewport(): array { - $response = $this->responseReader->waitForResponse(); + $response = $this->awaitResponse(); return $response->getResultData('layoutViewport'); } /** * Returns metrics relating to the visual viewport * @return array - * @throws LayoutMetricsFailed + * @throws CommunicationException\ResponseHasError + * @throws \HeadlessChromium\Exception\NoResponseAvailable + * @throws \HeadlessChromium\Exception\OperationTimedOut */ public function getVisualViewport() { - $response = $this->responseReader->waitForResponse(); + $response = $this->awaitResponse(); return $response->getResultData('visualViewport'); } } diff --git a/src/PageUtils/ResponseWaiter.php b/src/PageUtils/ResponseWaiter.php index 779f718f..86eb672b 100644 --- a/src/PageUtils/ResponseWaiter.php +++ b/src/PageUtils/ResponseWaiter.php @@ -5,6 +5,7 @@ namespace HeadlessChromium\PageUtils; +use HeadlessChromium\Communication\Response; use HeadlessChromium\Communication\ResponseReader; use HeadlessChromium\Exception\CommunicationException\ResponseHasError; @@ -16,6 +17,11 @@ class ResponseWaiter */ protected $responseReader; + /** + * @var Response + */ + protected $response; + /** * @param ResponseReader $responseReader */ @@ -25,6 +31,7 @@ public function __construct(ResponseReader $responseReader) } /** + * Chainable wait for response * @param $time * @throws \HeadlessChromium\Exception\NoResponseAvailable * @throws \HeadlessChromium\Exception\OperationTimedOut @@ -34,15 +41,32 @@ public function __construct(ResponseReader $responseReader) */ public function await(int $time = null) { - $response = $this->responseReader->waitForResponse($time); + $this->response = $this->responseReader->waitForResponse($time); - if (!$response->isSuccessful()) { - throw new ResponseHasError($response->getErrorMessage(true)); + if (!$this->response->isSuccessful()) { + throw new ResponseHasError($this->response->getErrorMessage(true)); } return $this; } + /** + * Waits for response and return it + * @param int|null $time + * @return Response + * @throws ResponseHasError + * @throws \HeadlessChromium\Exception\NoResponseAvailable + * @throws \HeadlessChromium\Exception\OperationTimedOut + */ + protected function awaitResponse(int $time = null): Response + { + if (!$this->response) { + $this->await($time); + } + + return $this->response; + } + /** * @return ResponseReader */