Completed
Push — master ( 1467e1...e09e40 )
by Nils
02:08
created

GoogleMobileFriendlyRule::validate()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 14
nc 3
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Seo;
4
5
use whm\Html\Uri;
6
use whm\Smoke\Http\Response;
7
use whm\Smoke\Rules\Rule;
8
use whm\Smoke\Rules\ValidationFailedException;
9
10
class GoogleMobileFriendlyRule implements Rule
11
{
12
    const ENDPOINT = 'https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url=#url#&strategy=mobile';
13
14
    private function getEndpoint(Uri $uri)
15
    {
16
        // return str_replace('#url#', urlencode('https://webhook.koalamon.com'), self::ENDPOINT);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
17
        return str_replace('#url#', urlencode((string) $uri), self::ENDPOINT);
18
    }
19
20
    public function validate(Response $response)
21
    {
22
        $uri = $response->getUri();
23
24
        $endpoint = $this->getEndpoint($uri);
25
26
        $result = json_decode(file_get_contents($endpoint, false,
27
            stream_context_create(
28
                array(
29
                    'http' => array(
30
                        'ignore_errors' => true,
31
                    ),
32
                )
33
            )));
34
35
        var_dump($result);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($result); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
36
37
        if ($result->error) {
38
            throw new ValidationFailedException('Google mobile friendly test was not passed. Error "' . $result->error->message . '"');
39
        }
40
41
        $passResult = $result->ruleGroups->USABILITY;
42
43
        if (!$passResult->pass) {
44
            throw new ValidationFailedException('Google mobile friendly test was not passed. Score ' . $passResult->score . '/100.');
45
        }
46
    }
47
}
48