From 26a61857dd438eaebb3d4daeadf953fd12f44ccc Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 27 Mar 2017 21:43:45 +0200 Subject: [PATCH 1/8] Add insights checker --- .env.example | 2 + .../GoogleInsightsServiceProvider.php | 32 +++ app/Services/Insights.php | 120 ++++++++++ composer.json | 3 +- composer.lock | 212 ++++++++++++------ config/app.php | 1 + config/services.php | 6 +- routes/api.php | 22 +- 8 files changed, 324 insertions(+), 74 deletions(-) create mode 100644 app/Providers/GoogleInsightsServiceProvider.php create mode 100644 app/Services/Insights.php diff --git a/.env.example b/.env.example index d445610..dc424c4 100644 --- a/.env.example +++ b/.env.example @@ -30,3 +30,5 @@ MAIL_ENCRYPTION=null PUSHER_APP_ID= PUSHER_KEY= PUSHER_SECRET= + +GOOGLE_KEY=AIzaSyD0OhFiimo2Wn604quZ8v95sMhA2JOOk8E \ No newline at end of file diff --git a/app/Providers/GoogleInsightsServiceProvider.php b/app/Providers/GoogleInsightsServiceProvider.php new file mode 100644 index 0000000..045c57b --- /dev/null +++ b/app/Providers/GoogleInsightsServiceProvider.php @@ -0,0 +1,32 @@ +app->singleton(InsightsCaller::class, function () { + return new InsightsCaller(config('services.google.key'), config('app.locale')); + }); + } +} diff --git a/app/Services/Insights.php b/app/Services/Insights.php new file mode 100644 index 0000000..a8ecc20 --- /dev/null +++ b/app/Services/Insights.php @@ -0,0 +1,120 @@ +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' => !!$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: " . $result->getSpeedScore() . '', + '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: " . $result->getUsabilityScore() . '', + '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: " . $result->getSpeedScore() . '', + 'help' => null, + 'level' => $this->getLevel(100 - $result->getSpeedScore()), + ]; + } + + + $results = collect($result->getFormattedResults()->getRuleResults()) + ->sortByDesc(function($result){ + return $result->getRuleImpact(); + }); + + /** + * @var string $rule + * @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()->toString() ?: $rule, + 'help' => $help ? implode('
', $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::WARNING; + } else { + return Levels::CRITICAL; + } + } +} diff --git a/composer.json b/composer.json index b5f90f5..1d35455 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index a71d248..48fcef5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "e0cb0127d710db79af564d28e94ae8df", + "hash": "082859116556eddbe8d07ef4b62a9d58", + "content-hash": "30a6b041d4441d90faeea4a2b5e65a16", "packages": [ { "name": "dnoegel/php-xdg-base-dir", @@ -37,7 +38,7 @@ "MIT" ], "description": "implementation of xdg base directory specification for php", - "time": "2014-10-24T07:27:01+00:00" + "time": "2014-10-24 07:27:01" }, { "name": "doctrine/inflector", @@ -104,7 +105,39 @@ "singularize", "string" ], - "time": "2015-11-06T14:35:42+00:00" + "time": "2015-11-06 14:35:42" + }, + { + "name": "dsentker/phpinsights", + "version": "0.2.1", + "source": { + "type": "git", + "url": "https://github.com/dsentker/phpinsights.git", + "reference": "825929c6a5e2270250fb05f05870b823b1dda0d4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dsentker/phpinsights/zipball/825929c6a5e2270250fb05f05870b823b1dda0d4", + "reference": "825929c6a5e2270250fb05f05870b823b1dda0d4", + "shasum": "" + }, + "require": { + "guzzlehttp/guzzle": "^6.2", + "netresearch/jsonmapper": "^1.1", + "php": "^5.4 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^5.7" + }, + "type": "library", + "autoload": { + "psr-4": { + "PhpInsights\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "description": "A php wrapper for Googles page speed insights", + "time": "2017-03-26 22:27:08" }, { "name": "erusev/parsedown", @@ -146,7 +179,7 @@ "markdown", "parser" ], - "time": "2016-11-02T15:56:58+00:00" + "time": "2016-11-02 15:56:58" }, { "name": "guzzlehttp/guzzle", @@ -208,7 +241,7 @@ "rest", "web service" ], - "time": "2016-10-08T15:01:37+00:00" + "time": "2016-10-08 15:01:37" }, { "name": "guzzlehttp/promises", @@ -259,7 +292,7 @@ "keywords": [ "promise" ], - "time": "2016-12-20T10:07:11+00:00" + "time": "2016-12-20 10:07:11" }, { "name": "guzzlehttp/psr7", @@ -324,7 +357,7 @@ "uri", "url" ], - "time": "2017-02-21T01:20:32+00:00" + "time": "2017-02-21 01:20:32" }, { "name": "jakub-onderka/php-console-color", @@ -367,7 +400,7 @@ "homepage": "http://www.acci.cz" } ], - "time": "2014-04-08T15:00:19+00:00" + "time": "2014-04-08 15:00:19" }, { "name": "jakub-onderka/php-console-highlighter", @@ -411,7 +444,7 @@ "homepage": "http://www.acci.cz/" } ], - "time": "2015-04-20T18:58:01+00:00" + "time": "2015-04-20 18:58:01" }, { "name": "laravel/framework", @@ -540,7 +573,7 @@ "framework", "laravel" ], - "time": "2017-02-15T14:31:32+00:00" + "time": "2017-02-15 14:31:32" }, { "name": "laravel/tinker", @@ -598,7 +631,7 @@ "laravel", "psysh" ], - "time": "2016-12-30T18:13:17+00:00" + "time": "2016-12-30 18:13:17" }, { "name": "league/flysystem", @@ -681,7 +714,7 @@ "sftp", "storage" ], - "time": "2017-02-09T11:33:58+00:00" + "time": "2017-02-09 11:33:58" }, { "name": "monolog/monolog", @@ -759,7 +792,7 @@ "logging", "psr-3" ], - "time": "2016-11-26T00:15:39+00:00" + "time": "2016-11-26 00:15:39" }, { "name": "mtdowling/cron-expression", @@ -803,7 +836,7 @@ "cron", "schedule" ], - "time": "2017-01-23T04:29:33+00:00" + "time": "2017-01-23 04:29:33" }, { "name": "nesbot/carbon", @@ -856,7 +889,46 @@ "datetime", "time" ], - "time": "2017-01-16T07:55:07+00:00" + "time": "2017-01-16 07:55:07" + }, + { + "name": "netresearch/jsonmapper", + "version": "v1.1.1", + "source": { + "type": "git", + "url": "https://github.com/cweiske/jsonmapper.git", + "reference": "8d0b6d5b9f5940757b44bc0ee2f930aae6d4a6c7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/cweiske/jsonmapper/zipball/8d0b6d5b9f5940757b44bc0ee2f930aae6d4a6c7", + "reference": "8d0b6d5b9f5940757b44bc0ee2f930aae6d4a6c7", + "shasum": "" + }, + "require-dev": { + "phpunit/phpunit": "4.2.*", + "squizlabs/php_codesniffer": "~1.5" + }, + "type": "library", + "autoload": { + "psr-0": { + "JsonMapper": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "OSL-3.0" + ], + "authors": [ + { + "name": "Christian Weiske", + "email": "cweiske@cweiske.de", + "homepage": "http://github.com/cweiske/jsonmapper/", + "role": "Developer" + } + ], + "description": "Map nested JSON structures onto PHP classes", + "time": "2017-01-27 21:01:41" }, { "name": "nikic/php-parser", @@ -907,7 +979,7 @@ "parser", "php" ], - "time": "2017-02-10T20:20:03+00:00" + "time": "2017-02-10 20:20:03" }, { "name": "paragonie/random_compat", @@ -955,7 +1027,7 @@ "pseudorandom", "random" ], - "time": "2016-11-07T23:38:38+00:00" + "time": "2016-11-07 23:38:38" }, { "name": "psr/http-message", @@ -1005,7 +1077,7 @@ "request", "response" ], - "time": "2016-08-06T14:39:51+00:00" + "time": "2016-08-06 14:39:51" }, { "name": "psr/log", @@ -1052,7 +1124,7 @@ "psr", "psr-3" ], - "time": "2016-10-10T12:19:37+00:00" + "time": "2016-10-10 12:19:37" }, { "name": "psy/psysh", @@ -1125,7 +1197,7 @@ "interactive", "shell" ], - "time": "2017-01-15T17:54:13+00:00" + "time": "2017-01-15 17:54:13" }, { "name": "ramsey/uuid", @@ -1207,7 +1279,7 @@ "identifier", "uuid" ], - "time": "2016-11-22T19:21:44+00:00" + "time": "2016-11-22 19:21:44" }, { "name": "swiftmailer/swiftmailer", @@ -1261,7 +1333,7 @@ "mail", "mailer" ], - "time": "2017-02-13T07:52:53+00:00" + "time": "2017-02-13 07:52:53" }, { "name": "symfony/console", @@ -1324,7 +1396,7 @@ ], "description": "Symfony Console Component", "homepage": "https://symfony.com", - "time": "2017-02-16T14:07:22+00:00" + "time": "2017-02-16 14:07:22" }, { "name": "symfony/css-selector", @@ -1377,7 +1449,7 @@ ], "description": "Symfony CssSelector Component", "homepage": "https://symfony.com", - "time": "2017-01-02T20:32:22+00:00" + "time": "2017-01-02 20:32:22" }, { "name": "symfony/debug", @@ -1434,7 +1506,7 @@ ], "description": "Symfony Debug Component", "homepage": "https://symfony.com", - "time": "2017-02-16T16:34:18+00:00" + "time": "2017-02-16 16:34:18" }, { "name": "symfony/dom-crawler", @@ -1490,7 +1562,7 @@ ], "description": "Symfony DomCrawler Component", "homepage": "https://symfony.com", - "time": "2017-01-21T17:14:11+00:00" + "time": "2017-01-21 17:14:11" }, { "name": "symfony/event-dispatcher", @@ -1550,7 +1622,7 @@ ], "description": "Symfony EventDispatcher Component", "homepage": "https://symfony.com", - "time": "2017-01-02T20:32:22+00:00" + "time": "2017-01-02 20:32:22" }, { "name": "symfony/finder", @@ -1599,7 +1671,7 @@ ], "description": "Symfony Finder Component", "homepage": "https://symfony.com", - "time": "2017-01-02T20:32:22+00:00" + "time": "2017-01-02 20:32:22" }, { "name": "symfony/http-foundation", @@ -1652,7 +1724,7 @@ ], "description": "Symfony HttpFoundation Component", "homepage": "https://symfony.com", - "time": "2017-02-16T22:46:52+00:00" + "time": "2017-02-16 22:46:52" }, { "name": "symfony/http-kernel", @@ -1734,7 +1806,7 @@ ], "description": "Symfony HttpKernel Component", "homepage": "https://symfony.com", - "time": "2017-02-16T23:59:56+00:00" + "time": "2017-02-16 23:59:56" }, { "name": "symfony/polyfill-mbstring", @@ -1793,7 +1865,7 @@ "portable", "shim" ], - "time": "2016-11-14T01:06:16+00:00" + "time": "2016-11-14 01:06:16" }, { "name": "symfony/process", @@ -1842,7 +1914,7 @@ ], "description": "Symfony Process Component", "homepage": "https://symfony.com", - "time": "2017-02-16T14:07:22+00:00" + "time": "2017-02-16 14:07:22" }, { "name": "symfony/routing", @@ -1917,7 +1989,7 @@ "uri", "url" ], - "time": "2017-01-28T02:37:08+00:00" + "time": "2017-01-28 02:37:08" }, { "name": "symfony/translation", @@ -1981,7 +2053,7 @@ ], "description": "Symfony Translation Component", "homepage": "https://symfony.com", - "time": "2017-02-16T22:46:52+00:00" + "time": "2017-02-16 22:46:52" }, { "name": "symfony/var-dumper", @@ -2044,7 +2116,7 @@ "debug", "dump" ], - "time": "2017-02-16T22:46:52+00:00" + "time": "2017-02-16 22:46:52" }, { "name": "t1gor/robots-txt-parser", @@ -2100,7 +2172,7 @@ "robots.txt", "yandex" ], - "time": "2016-08-10T19:04:18+00:00" + "time": "2016-08-10 19:04:18" }, { "name": "tijsverkoyen/css-to-inline-styles", @@ -2147,7 +2219,7 @@ ], "description": "CssToInlineStyles is a class that enables you to convert HTML-pages/files into HTML-pages/files with inline styles. This is very useful when you're sending emails.", "homepage": "https://github.com/tijsverkoyen/CssToInlineStyles", - "time": "2016-09-20T12:50:39+00:00" + "time": "2016-09-20 12:50:39" }, { "name": "vlucas/phpdotenv", @@ -2197,7 +2269,7 @@ "env", "environment" ], - "time": "2016-09-01T10:05:43+00:00" + "time": "2016-09-01 10:05:43" } ], "packages-dev": [ @@ -2265,7 +2337,7 @@ "phpstorm", "sublime" ], - "time": "2017-02-22T12:27:33+00:00" + "time": "2017-02-22 12:27:33" }, { "name": "barryvdh/reflection-docblock", @@ -2314,7 +2386,7 @@ "email": "mike.vanriel@naenius.com" } ], - "time": "2016-06-13T19:28:20+00:00" + "time": "2016-06-13 19:28:20" }, { "name": "doctrine/instantiator", @@ -2368,7 +2440,7 @@ "constructor", "instantiate" ], - "time": "2015-06-14T21:17:01+00:00" + "time": "2015-06-14 21:17:01" }, { "name": "fzaninotto/faker", @@ -2416,7 +2488,7 @@ "faker", "fixtures" ], - "time": "2016-04-29T12:21:54+00:00" + "time": "2016-04-29 12:21:54" }, { "name": "hamcrest/hamcrest-php", @@ -2461,7 +2533,7 @@ "keywords": [ "test" ], - "time": "2015-05-11T14:41:42+00:00" + "time": "2015-05-11 14:41:42" }, { "name": "laravel/browser-kit-testing", @@ -2508,7 +2580,7 @@ "laravel", "testing" ], - "time": "2017-02-08T22:32:37+00:00" + "time": "2017-02-08 22:32:37" }, { "name": "mockery/mockery", @@ -2573,7 +2645,7 @@ "test double", "testing" ], - "time": "2017-02-09T13:29:38+00:00" + "time": "2017-02-09 13:29:38" }, { "name": "myclabs/deep-copy", @@ -2615,7 +2687,7 @@ "object", "object graph" ], - "time": "2017-01-26T22:05:40+00:00" + "time": "2017-01-26 22:05:40" }, { "name": "phpdocumentor/reflection-common", @@ -2669,7 +2741,7 @@ "reflection", "static analysis" ], - "time": "2015-12-27T11:43:31+00:00" + "time": "2015-12-27 11:43:31" }, { "name": "phpdocumentor/reflection-docblock", @@ -2714,7 +2786,7 @@ } ], "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", - "time": "2016-09-30T07:12:33+00:00" + "time": "2016-09-30 07:12:33" }, { "name": "phpdocumentor/type-resolver", @@ -2761,7 +2833,7 @@ "email": "me@mikevanriel.com" } ], - "time": "2016-11-25T06:54:22+00:00" + "time": "2016-11-25 06:54:22" }, { "name": "phpspec/prophecy", @@ -2824,7 +2896,7 @@ "spy", "stub" ], - "time": "2016-11-21T14:58:47+00:00" + "time": "2016-11-21 14:58:47" }, { "name": "phpunit/php-code-coverage", @@ -2887,7 +2959,7 @@ "testing", "xunit" ], - "time": "2017-01-20T15:06:43+00:00" + "time": "2017-01-20 15:06:43" }, { "name": "phpunit/php-file-iterator", @@ -2934,7 +3006,7 @@ "filesystem", "iterator" ], - "time": "2016-10-03T07:40:28+00:00" + "time": "2016-10-03 07:40:28" }, { "name": "phpunit/php-text-template", @@ -2975,7 +3047,7 @@ "keywords": [ "template" ], - "time": "2015-06-21T13:50:34+00:00" + "time": "2015-06-21 13:50:34" }, { "name": "phpunit/php-timer", @@ -3019,7 +3091,7 @@ "keywords": [ "timer" ], - "time": "2016-05-12T18:03:57+00:00" + "time": "2016-05-12 18:03:57" }, { "name": "phpunit/php-token-stream", @@ -3068,7 +3140,7 @@ "keywords": [ "tokenizer" ], - "time": "2016-11-15T14:06:22+00:00" + "time": "2016-11-15 14:06:22" }, { "name": "phpunit/phpunit", @@ -3150,7 +3222,7 @@ "testing", "xunit" ], - "time": "2017-02-19T07:22:16+00:00" + "time": "2017-02-19 07:22:16" }, { "name": "phpunit/phpunit-mock-objects", @@ -3209,7 +3281,7 @@ "mock", "xunit" ], - "time": "2016-12-08T20:27:08+00:00" + "time": "2016-12-08 20:27:08" }, { "name": "sebastian/code-unit-reverse-lookup", @@ -3254,7 +3326,7 @@ ], "description": "Looks up which function or method a line of code belongs to", "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", - "time": "2016-02-13T06:45:14+00:00" + "time": "2016-02-13 06:45:14" }, { "name": "sebastian/comparator", @@ -3318,7 +3390,7 @@ "compare", "equality" ], - "time": "2017-01-29T09:50:25+00:00" + "time": "2017-01-29 09:50:25" }, { "name": "sebastian/diff", @@ -3370,7 +3442,7 @@ "keywords": [ "diff" ], - "time": "2015-12-08T07:14:41+00:00" + "time": "2015-12-08 07:14:41" }, { "name": "sebastian/environment", @@ -3420,7 +3492,7 @@ "environment", "hhvm" ], - "time": "2016-11-26T07:53:53+00:00" + "time": "2016-11-26 07:53:53" }, { "name": "sebastian/exporter", @@ -3487,7 +3559,7 @@ "export", "exporter" ], - "time": "2016-11-19T08:54:04+00:00" + "time": "2016-11-19 08:54:04" }, { "name": "sebastian/global-state", @@ -3538,7 +3610,7 @@ "keywords": [ "global state" ], - "time": "2015-10-12T03:26:01+00:00" + "time": "2015-10-12 03:26:01" }, { "name": "sebastian/object-enumerator", @@ -3584,7 +3656,7 @@ ], "description": "Traverses array structures and object graphs to enumerate all referenced objects", "homepage": "https://github.com/sebastianbergmann/object-enumerator/", - "time": "2017-02-18T15:18:39+00:00" + "time": "2017-02-18 15:18:39" }, { "name": "sebastian/recursion-context", @@ -3637,7 +3709,7 @@ ], "description": "Provides functionality to recursively process PHP variables", "homepage": "http://www.github.com/sebastianbergmann/recursion-context", - "time": "2016-11-19T07:33:16+00:00" + "time": "2016-11-19 07:33:16" }, { "name": "sebastian/resource-operations", @@ -3679,7 +3751,7 @@ ], "description": "Provides a list of PHP built-in functions that operate on resources", "homepage": "https://www.github.com/sebastianbergmann/resource-operations", - "time": "2015-07-28T20:34:47+00:00" + "time": "2015-07-28 20:34:47" }, { "name": "sebastian/version", @@ -3722,7 +3794,7 @@ ], "description": "Library that helps with managing the version number of Git-hosted PHP projects", "homepage": "https://github.com/sebastianbergmann/version", - "time": "2016-10-03T07:35:21+00:00" + "time": "2016-10-03 07:35:21" }, { "name": "symfony/class-loader", @@ -3778,7 +3850,7 @@ ], "description": "Symfony ClassLoader Component", "homepage": "https://symfony.com", - "time": "2017-01-21T17:06:35+00:00" + "time": "2017-01-21 17:06:35" }, { "name": "symfony/yaml", @@ -3833,7 +3905,7 @@ ], "description": "Symfony Yaml Component", "homepage": "https://symfony.com", - "time": "2017-02-16T22:46:52+00:00" + "time": "2017-02-16 22:46:52" }, { "name": "webmozart/assert", @@ -3883,7 +3955,7 @@ "check", "validate" ], - "time": "2016-11-23T20:04:58+00:00" + "time": "2016-11-23 20:04:58" } ], "aliases": [], diff --git a/config/app.php b/config/app.php index 0789450..66807ba 100644 --- a/config/app.php +++ b/config/app.php @@ -185,6 +185,7 @@ App\Providers\UrlFetcherServiceProvider::class, App\Providers\RobotsFileServiceProvider::class, App\Providers\MarkdownServiceProvider::class, + App\Providers\GoogleInsightsServiceProvider::class, ], diff --git a/config/services.php b/config/services.php index 6bb0952..876f5d1 100644 --- a/config/services.php +++ b/config/services.php @@ -14,7 +14,11 @@ | */ - 'mailgun' => [ + 'google' => [ + 'key' => env('GOOGLE_KEY'), + ], + + 'insights' => [ 'domain' => env('MAILGUN_DOMAIN'), 'secret' => env('MAILGUN_SECRET'), ], diff --git a/routes/api.php b/routes/api.php index beca092..9c24248 100644 --- a/routes/api.php +++ b/routes/api.php @@ -2,9 +2,27 @@ use App\Http\Requests\CheckRequest; use App\Services\Checker; +use App\Services\Insights; +use PhpInsights\InsightsCaller; +use App\Rules\Levels; -Route::get('check', function (CheckRequest $request, Checker $checker) { +Route::get('check', function (CheckRequest $request, Checker $checker, Insights $insights) { $results = $checker->validate($request->url); - return response()->json(iterator_to_array($results)); + $results = iterator_to_array($results); + + if (config('services.google.key')) { + $insightResults = $insights->validate($request->url, InsightsCaller::STRATEGY_MOBILE); + + $results = array_merge($results, iterator_to_array($insightResults)); + + $insightResults = $insights->validate($request->url, InsightsCaller::STRATEGY_DESKTOP); + + $results = array_merge($results, iterator_to_array($insightResults)); + + // Remove duplicate entries + $results = array_map("unserialize", array_unique(array_map("serialize", $results))); + } + + return response()->json($results); })->name('check'); From 034b364e45d411b2106db1d748623ac766bc9ed1 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 27 Mar 2017 21:44:47 +0200 Subject: [PATCH 2/8] remove example key --- .env.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.example b/.env.example index dc424c4..c53b221 100644 --- a/.env.example +++ b/.env.example @@ -31,4 +31,4 @@ PUSHER_APP_ID= PUSHER_KEY= PUSHER_SECRET= -GOOGLE_KEY=AIzaSyD0OhFiimo2Wn604quZ8v95sMhA2JOOk8E \ No newline at end of file +GOOGLE_KEY= \ No newline at end of file From 131bec09bb11b9fc6f5e7c0817b34011c6138297 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 27 Mar 2017 21:46:13 +0200 Subject: [PATCH 3/8] Fix services --- config/services.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/services.php b/config/services.php index 876f5d1..3a8192e 100644 --- a/config/services.php +++ b/config/services.php @@ -18,7 +18,7 @@ 'key' => env('GOOGLE_KEY'), ], - 'insights' => [ + 'mailgun' => [ 'domain' => env('MAILGUN_DOMAIN'), 'secret' => env('MAILGUN_SECRET'), ], From 7149d4718b1ea1cd5cd8c625824a0b5f3abb8bcf Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 27 Mar 2017 21:56:29 +0200 Subject: [PATCH 4/8] CS fix --- .../GoogleInsightsServiceProvider.php | 1 - app/Services/Insights.php | 20 ++++++++----------- routes/api.php | 3 +-- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/app/Providers/GoogleInsightsServiceProvider.php b/app/Providers/GoogleInsightsServiceProvider.php index 045c57b..83dba51 100644 --- a/app/Providers/GoogleInsightsServiceProvider.php +++ b/app/Providers/GoogleInsightsServiceProvider.php @@ -2,7 +2,6 @@ namespace App\Providers; -use App\Services\UrlFetcher; use Illuminate\Support\ServiceProvider; use PhpInsights\InsightsCaller; diff --git a/app/Services/Insights.php b/app/Services/Insights.php index a8ecc20..1426d9f 100644 --- a/app/Services/Insights.php +++ b/app/Services/Insights.php @@ -16,7 +16,7 @@ class Insights /** * Construct a new instance of this service. * - * @param InsightsCaller $insightsCaller + * @param InsightsCaller $insightsCaller */ public function __construct(InsightsCaller $insightsCaller) { @@ -27,7 +27,7 @@ public function __construct(InsightsCaller $insightsCaller) * Validate the URL against our checklist. * * @param string|null $url - * @param string $strategy + * @param string $strategy * * @throws Exception * @@ -42,7 +42,7 @@ public function validate($url, $strategy = InsightsCaller::STRATEGY_MOBILE) yield [ // If the rule impact is zero, it means that the website has passed the test. - 'passed' => !!$result->screenshot, + 'passed' => (bool) $result->screenshot, 'message' => $result->screenshot->getImageHtml(), 'help' => null, 'level' => Levels::NOTICE, @@ -52,7 +52,7 @@ public function validate($url, $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: " . $result->getSpeedScore() . '', + 'message' => "Your Google Page Insights Mobile Pagespeed score is: ".$result->getSpeedScore().'', 'help' => null, 'level' => $this->getLevel(100 - $result->getSpeedScore()), ]; @@ -60,7 +60,7 @@ public function validate($url, $strategy = InsightsCaller::STRATEGY_MOBILE) 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: " . $result->getUsabilityScore() . '', + 'message' => "Your Google Page Insights Mobile Usability score is: ".$result->getUsabilityScore().'', 'help' => null, 'level' => $this->getLevel(100 - $result->getUsabilityScore()), ]; @@ -74,17 +74,13 @@ public function validate($url, $strategy = InsightsCaller::STRATEGY_MOBILE) ]; } - $results = collect($result->getFormattedResults()->getRuleResults()) - ->sortByDesc(function($result){ + ->sortByDesc(function ($result){ return $result->getRuleImpact(); }); - /** - * @var string $rule - * @var DefaultRuleResult $ruleResult - */ - foreach($results as $rule => $ruleResult) { + /** @var DefaultRuleResult $ruleResult */ + foreach ($results as $rule => $ruleResult) { $help = []; if ($urlBlocks = $ruleResult->getUrlBlocks()) { diff --git a/routes/api.php b/routes/api.php index 9c24248..0f59cc8 100644 --- a/routes/api.php +++ b/routes/api.php @@ -4,7 +4,6 @@ use App\Services\Checker; use App\Services\Insights; use PhpInsights\InsightsCaller; -use App\Rules\Levels; Route::get('check', function (CheckRequest $request, Checker $checker, Insights $insights) { $results = $checker->validate($request->url); @@ -21,7 +20,7 @@ $results = array_merge($results, iterator_to_array($insightResults)); // Remove duplicate entries - $results = array_map("unserialize", array_unique(array_map("serialize", $results))); + $results = array_map('unserialize', array_unique(array_map('serialize', $results))); } return response()->json($results); From 54232d959dbafa4c243015dcf3b467182332e955 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Mon, 27 Mar 2017 21:59:17 +0200 Subject: [PATCH 5/8] CS fix --- app/Services/Insights.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Services/Insights.php b/app/Services/Insights.php index 1426d9f..ec38e04 100644 --- a/app/Services/Insights.php +++ b/app/Services/Insights.php @@ -52,7 +52,7 @@ public function validate($url, $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: ".$result->getSpeedScore().'', + 'message' => 'Your Google Page Insights Mobile Pagespeed score is: '.$result->getSpeedScore().'', 'help' => null, 'level' => $this->getLevel(100 - $result->getSpeedScore()), ]; @@ -60,7 +60,7 @@ public function validate($url, $strategy = InsightsCaller::STRATEGY_MOBILE) 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: ".$result->getUsabilityScore().'', + 'message' => 'Your Google Page Insights Mobile Usability score is: '.$result->getUsabilityScore().'', 'help' => null, 'level' => $this->getLevel(100 - $result->getUsabilityScore()), ]; @@ -68,14 +68,14 @@ public function validate($url, $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 Desktop Pagespeed score is: " . $result->getSpeedScore() . '', + 'message' => 'Your Google Page Insights Desktop Pagespeed score is: '.$result->getSpeedScore().'', 'help' => null, 'level' => $this->getLevel(100 - $result->getSpeedScore()), ]; } $results = collect($result->getFormattedResults()->getRuleResults()) - ->sortByDesc(function ($result){ + ->sortByDesc(function (DefaultRuleResult $result) { return $result->getRuleImpact(); }); From e6b907054da3e55766e77ee00a0d8999f8386dc6 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 2 Apr 2017 09:09:53 +0200 Subject: [PATCH 6/8] cs fix --- app/Services/Insights.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Services/Insights.php b/app/Services/Insights.php index ec38e04..986b041 100644 --- a/app/Services/Insights.php +++ b/app/Services/Insights.php @@ -81,7 +81,6 @@ public function validate($url, $strategy = InsightsCaller::STRATEGY_MOBILE) /** @var DefaultRuleResult $ruleResult */ foreach ($results as $rule => $ruleResult) { - $help = []; if ($urlBlocks = $ruleResult->getUrlBlocks()) { foreach ($ruleResult->getDetails() as $detail) { From e3efc2ee87053101cd18d515a8c22542308570ed Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Fri, 7 Apr 2017 09:50:26 +0200 Subject: [PATCH 7/8] Check summary exists --- app/Services/Insights.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Services/Insights.php b/app/Services/Insights.php index 986b041..e1bcba4 100644 --- a/app/Services/Insights.php +++ b/app/Services/Insights.php @@ -93,7 +93,7 @@ public function validate($url, $strategy = InsightsCaller::STRATEGY_MOBILE) yield [ // If the rule impact is zero, it means that the website has passed the test. 'passed' => $ruleResult->getRuleImpact() == 0, - 'message' => $ruleResult->getSummary()->toString() ?: $rule, + 'message' => $ruleResult->getSummary() ? $ruleResult->getSummary()->toString() : $rule, 'help' => $help ? implode('
', $help) : null, 'level' => $this->getLevel($ruleResult->getRuleImpact()), ]; From d0fdef0422e719dc1214d7531525bf650d0d3c56 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Fri, 7 Apr 2017 11:42:09 +0200 Subject: [PATCH 8/8] Use error instead of warning --- app/Services/Insights.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Services/Insights.php b/app/Services/Insights.php index e1bcba4..f906f08 100644 --- a/app/Services/Insights.php +++ b/app/Services/Insights.php @@ -107,7 +107,7 @@ protected function getLevel($impact) } elseif ($impact < 20) { return Levels::WARNING; } elseif ($impact < 50) { - return Levels::WARNING; + return Levels::ERROR; } else { return Levels::CRITICAL; }