Skip to content

Commit

Permalink
Move things
Browse files Browse the repository at this point in the history
  • Loading branch information
smnandre committed May 15, 2024
1 parent 61d5a3c commit 1b43393
Show file tree
Hide file tree
Showing 14 changed files with 15,886 additions and 67 deletions.
83 changes: 28 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
# PageSeed Api

This library provides a simple way to interact with the Google PageSpeed Insights API.

```php
use PageSpeed\Api\Analysis\Category;use PageSpeed\Api\PageSpeedApi;

$api = new PageSpeedApi('YOUR_API');

$analysis = $api->analyse('https://www.example.com');

// LightHouse scores
echo $audit->getPerformancesScore(); // 100
echo $audit->getAccessibilityScore(); // 88
echo $audit->getBestPracticesScore(); // 100
echo $audit->getSeoScore(); // 90

// Loading Experience metrics
echo $audit->getLargestContentfulPaint(); //
echo $audit->getInteractiveToNextPaint(); //
echo $audit->getCumulativeLayoutShift(); //
echo $audit->getFirstContentfulPaint(); //
echo $audit->getFirstInputDelay(); //
echo $audit->getTimeToFirstByte(); //
```
Google PageSpeed Insights API wrapper for PHP.

## Installation

Expand All @@ -32,54 +10,49 @@ composer require smnandre/pagespeed-api

## Usage


### PageSpeed Api Key
### Run an analysis

```php
use PageSpeed\Api\PageSpeedApi;
use PageSpeed\Api\Analysis\Category;use PageSpeed\Api\PageSpeedApi;

$api = new PageSpeedApi('YOUR_API');
```

### Parameters

#### Audit Strategy

```php
// Mobile strategy (default)
$audit = $api->analyse('https://example.com/', 'mobile');

// Desktop strategy
$audit = $api->analyse('https://example.com/', 'desktop');
$analysis = $api->analyse('https://www.example.com');
```

#### Analysis Categories


### Response

#### Analysis
### Get the results

```php

#### Loading Experience
// LightHouse scores
$analysis->getAuditScores();
// 'performance' => 100,
// 'accessibility' => 88,
// 'best-practices' => 100,
// 'seo' => 90

// Loading Experience metrics
$analysis->getLoadingMetrics();

#### Original Loading Experience
// Origin Loading Experience metrics
$analysis->getOriginalLoadingMetrics();
```

..
### Parameters

#### Lighthouse
#### Strategy

```php
use PageSpeed\Api\PageSpeedApi;
// Mobile strategy (default)
$analysis = $api->analyse('https://example.com/', 'mobile');

$api = new PageSpeedApi();
// Desktop strategy
$analysis = $api->analyse('https://example.com/', 'desktop');
```

$audit = $api->audit('https://www.example.com');
#### Locale

echo $audit->getScore();
echo $audit->performanceScore();
echo $audit->getAccessibilityScore();
echo $audit->getBestPracticesScore();
echo $audit->getSeoScore();
```php
$analysis = $api->analyse('https://example.com/', 'mobile', 'fr');
```

27 changes: 27 additions & 0 deletions src/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace PageSpeed\Api;

use DateTimeImmutable;
use PageSpeed\Api\Analysis\LighthouseResult;
use PageSpeed\Api\Analysis\LoadingExperience;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -65,11 +66,37 @@ public static function create(array $values): self
);
}

public function getId(): string
{
return $this->id;
}

/**
* @return array<string, int>
*/
public function getAuditScores(): array
{
return $this->lighthouseResult->getScores();
}

/**
* @return array<string, mixed>|null
*/
public function getLoadingMetrics(): ?array
{
return $this->loadingExperience?->getMetrics();
}

/**
* @return array<string, mixed>|null
*/
public function getOriginalLoadingMetrics(): ?array
{
return $this->originLoadingExperience?->getMetrics();
}

public function getAnalysisUtcDateTime(): DateTimeImmutable
{
return new DateTimeImmutable($this->analysisUTCTimestamp);
}
}
11 changes: 10 additions & 1 deletion src/Analysis/LoadingExperience.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
final readonly class LoadingExperience
{
/**
* @param array<string, mixed> $metrics
* @param array<string, array<string, mixed>> $metrics
*/
public function __construct(
public string $id,
Expand Down Expand Up @@ -52,4 +52,13 @@ public static function create(array $values): self
$values['initial_url'],
);
}

/**
* @return array<string, string>
*/
public function getMetrics(): array
{
/** @phpstan-ignore-next-line */
return array_map(fn (array $metric) => $metric['category'], $this->metrics);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* file that was distributed with this source code.
*/

namespace PageSpeed\Api\Tests\Fixtures;
namespace PageSpeed\Api\Tests\Fixtures\Factory;

class AnalysisFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* file that was distributed with this source code.
*/

namespace PageSpeed\Api\Tests\Fixtures;
namespace PageSpeed\Api\Tests\Fixtures\Factory;

class ConfigSettingsFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* file that was distributed with this source code.
*/

namespace PageSpeed\Api\Tests\Fixtures;
namespace PageSpeed\Api\Tests\Fixtures\Factory;

class EnvironmentFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* file that was distributed with this source code.
*/

namespace PageSpeed\Api\Tests\Fixtures;
namespace PageSpeed\Api\Tests\Fixtures\Factory;

class LighthouseCategoryResultFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* file that was distributed with this source code.
*/

namespace PageSpeed\Api\Tests\Fixtures;
namespace PageSpeed\Api\Tests\Fixtures\Factory;

class LighthouseResultFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* file that was distributed with this source code.
*/

namespace PageSpeed\Api\Tests\Fixtures;
namespace PageSpeed\Api\Tests\Fixtures\Factory;

class LoadingExperienceFactory
{
Expand Down
Loading

0 comments on commit 1b43393

Please sign in to comment.