Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_KEY=
PUSHER_SECRET=

GOOGLE_KEY=
31 changes: 31 additions & 0 deletions app/Providers/GoogleInsightsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use PhpInsights\InsightsCaller;

class GoogleInsightsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(InsightsCaller::class, function () {
return new InsightsCaller(config('services.google.key'), config('app.locale'));
});
}
}
115 changes: 115 additions & 0 deletions app/Services/Insights.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace App\Services;

use App\Rules\Levels;
use Exception;
use GuzzleHttp\Psr7\Uri;
use PhpInsights\InsightsCaller;
use PhpInsights\Result\Map\FormattedResults\DefaultRuleResult;

class Insights
{
/** @var InsightsCaller */
private $insightsCaller;

/**
* Construct a new instance of this service.
*
* @param InsightsCaller $insightsCaller
*/
public function __construct(InsightsCaller $insightsCaller)
{
$this->insightsCaller = $insightsCaller;
}

/**
* Validate the URL against our checklist.
*
* @param string|null $url
* @param string $strategy
*
* @throws Exception
*
* @return \Generator
*/
public function validate($url, $strategy = InsightsCaller::STRATEGY_MOBILE)
{
$uri = new Uri($url);

$response = $this->insightsCaller->getResponse((string) $uri, $strategy);
$result = $response->getMappedResult();

yield [
// If the rule impact is zero, it means that the website has passed the test.
'passed' => (bool) $result->screenshot,
'message' => $result->screenshot->getImageHtml(),
'help' => null,
'level' => Levels::NOTICE,
];

if ($strategy == InsightsCaller::STRATEGY_MOBILE) {
yield [
// If the rule impact is zero, it means that the website has passed the test.
'passed' => $result->getSpeedScore() >= 80,
'message' => 'Your Google Page Insights Mobile Pagespeed score is: <b>'.$result->getSpeedScore().'</b>',
'help' => null,
'level' => $this->getLevel(100 - $result->getSpeedScore()),
];

yield [
// If the rule impact is zero, it means that the website has passed the test.
'passed' => $result->getUsabilityScore() >= 80,
'message' => 'Your Google Page Insights Mobile Usability score is: <b>'.$result->getUsabilityScore().'</b>',
'help' => null,
'level' => $this->getLevel(100 - $result->getUsabilityScore()),
];
} else {
yield [
// If the rule impact is zero, it means that the website has passed the test.
'passed' => $result->getSpeedScore() >= 80,
'message' => 'Your Google Page Insights Desktop Pagespeed score is: <b>'.$result->getSpeedScore().'</b>',
'help' => null,
'level' => $this->getLevel(100 - $result->getSpeedScore()),
];
}

$results = collect($result->getFormattedResults()->getRuleResults())
->sortByDesc(function (DefaultRuleResult $result) {
return $result->getRuleImpact();
});

/** @var DefaultRuleResult $ruleResult */
foreach ($results as $rule => $ruleResult) {
$help = [];
if ($urlBlocks = $ruleResult->getUrlBlocks()) {
foreach ($ruleResult->getDetails() as $detail) {
if ($detail) {
$help[] = $detail->toString();
}
}
}

yield [
// If the rule impact is zero, it means that the website has passed the test.
'passed' => $ruleResult->getRuleImpact() == 0,
'message' => $ruleResult->getSummary() ? $ruleResult->getSummary()->toString() : $rule,
'help' => $help ? implode('<br/>', $help) : null,
'level' => $this->getLevel($ruleResult->getRuleImpact()),
];
}
}

protected function getLevel($impact)
{
if ($impact < 3) {
return Levels::NOTICE;
} elseif ($impact < 20) {
return Levels::WARNING;
} elseif ($impact < 50) {
return Levels::ERROR;
} else {
return Levels::CRITICAL;
}
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"symfony/dom-crawler": "^3.1",
"t1gor/robots-txt-parser": "^0.2.3",
"erusev/parsedown": "^1.6",
"laravel/tinker": "^1.0"
"laravel/tinker": "^1.0",
"dsentker/phpinsights": "0.2.x"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down
Loading