Skip to content
Snippets Groups Projects
Commit 9690a03d authored by Soufiane Ghzal's avatar Soufiane Ghzal
Browse files

Replace LayoutMetricFailed in #44

parent 55d0ad0b
No related merge requests found
<?php
/**
* @license see LICENSE
*/
namespace HeadlessChromium\Exception;
class LayoutMetricsFailed extends \Exception
{
}
......@@ -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');
}
}
......@@ -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 @@ class ResponseWaiter
}
/**
* Chainable wait for response
* @param $time
* @throws \HeadlessChromium\Exception\NoResponseAvailable
* @throws \HeadlessChromium\Exception\OperationTimedOut
......@@ -34,15 +41,32 @@ class ResponseWaiter
*/
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
*/
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment