From 9690a03db13ee27984f633a84dcd9dc7d6ee4f5d Mon Sep 17 00:00:00 2001 From: Soufiane Ghzal Date: Thu, 25 Oct 2018 18:52:23 +0200 Subject: [PATCH] Replace LayoutMetricFailed in #44 --- src/Exception/LayoutMetricsFailed.php | 11 ----- src/PageUtils/PageLayoutMetrics.php | 62 ++++++++------------------- src/PageUtils/ResponseWaiter.php | 30 +++++++++++-- 3 files changed, 45 insertions(+), 58 deletions(-) delete mode 100644 src/Exception/LayoutMetricsFailed.php 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 */