Skip to content

Commit

Permalink
Replace LayoutMetricFailed in #44
Browse files Browse the repository at this point in the history
  • Loading branch information
gsouf committed Oct 25, 2018
1 parent 55d0ad0 commit 9690a03
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 58 deletions.
11 changes: 0 additions & 11 deletions src/Exception/LayoutMetricsFailed.php

This file was deleted.

62 changes: 18 additions & 44 deletions src/PageUtils/PageLayoutMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,90 +2,64 @@

namespace HeadlessChromium\PageUtils;

use HeadlessChromium\Communication\Response;
use HeadlessChromium\Communication\ResponseReader;
use HeadlessChromium\Exception\LayoutMetricsFailed;
use HeadlessChromium\Exception\CommunicationException;

/**
* Used to read layout metrics of the page
* @internal
*/
class PageLayoutMetrics
class PageLayoutMetrics extends ResponseWaiter
{

/**
* @var ResponseReader
*/
protected $responseReader;

/**
* @var Response
*/
protected $response;

public function __construct(ResponseReader $responseReader)
{
$this->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');
}
}
30 changes: 27 additions & 3 deletions src/PageUtils/ResponseWaiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace HeadlessChromium\PageUtils;

use HeadlessChromium\Communication\Response;
use HeadlessChromium\Communication\ResponseReader;
use HeadlessChromium\Exception\CommunicationException\ResponseHasError;

Expand All @@ -16,6 +17,11 @@ class ResponseWaiter
*/
protected $responseReader;

/**
* @var Response
*/
protected $response;

/**
* @param ResponseReader $responseReader
*/
Expand All @@ -25,6 +31,7 @@ public function __construct(ResponseReader $responseReader)
}

/**
* Chainable wait for response
* @param $time
* @throws \HeadlessChromium\Exception\NoResponseAvailable
* @throws \HeadlessChromium\Exception\OperationTimedOut
Expand All @@ -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
*/
Expand Down

0 comments on commit 9690a03

Please sign in to comment.