Completed
Push — master ( 83fedd...20342f )
by Nils
03:00
created

ValidRule::validateBody()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 13

Duplication

Lines 5
Ratio 29.41 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 5
loc 17
rs 9.4285
cc 3
eloc 13
nc 3
nop 2
1
<?php
2
3
namespace whm\Smoke\Rules\Xml\Sitemap;
4
5
use whm\Smoke\Http\Response;
6
use whm\Smoke\Rules\Rule;
7
use whm\Smoke\Rules\StandardRule;
8
use whm\Smoke\Rules\ValidationFailedException;
9
10
/**
11
 * This rule checks if a sitemap.xml file is valid.
12
 */
13
class ValidRule extends StandardRule
14
{
15
    const SCHEMA = 'schema.xsd';
16
17
    protected $contentTypes = array('text/xml');
18
19
    private function getSchema()
20
    {
21
        return __DIR__ . '/' . self::SCHEMA;
22
    }
23
24
    private function validateBody($body, $filename)
25
    {
26
        libxml_clear_errors();
27
        $dom = new \DOMDocument();
28
        @$dom->loadXML($body);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
29
        $lastError = libxml_get_last_error();
30
        if ($lastError) {
31
            throw new ValidationFailedException(
32
                'The given sitemap file (' . $filename . ') is not well formed (last error: ' . str_replace("\n", '', $lastError->message) . ').');
33
        }
34
        $valid = @$dom->schemaValidate($this->getSchema());
35 View Code Duplication
        if (!$valid) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
            $lastError = libxml_get_last_error();
37
            throw new ValidationFailedException(
38
                'The given sitemap file (' . $filename . ') did not validate against the sitemap.xsd (last error: ' . str_replace("\n", '', $lastError->message) . ').');
39
        }
40
    }
41
42
    /**
43
     * @param string
44
     *
45
     * @return array
46
     */
47
    private function getLocations($body)
48
    {
49
        $locations = array();
50
        $xml = simplexml_load_string($body);
51
        $json = json_encode($xml);
52
        $xmlValues = json_decode($json, true);
53
54
        if (isset($xmlValues['sitemap']['loc'])) {
55
            $locations[] = $xmlValues['sitemap']['loc'];
56
        } else {
57
            foreach ($xmlValues['sitemap'] as $sitemap) {
58
                $locations[] = $sitemap['loc'];
59
            }
60
        }
61
62
        return $locations;
63
    }
64
65
    protected function doValidation(Response $response)
66
    {
67
        $body = $response->getBody();
68
69
        // sitemapindex or urlset
70
        if (preg_match('/<sitemapindex/', $body)) {
71
            $allSingleSitemapsUrls = $this->getLocations($body);
72
            if (count($allSingleSitemapsUrls) > 0) {
73
                // we only check the first sitemap we find
74
                $this->validateBody(file_get_contents($allSingleSitemapsUrls[0]), $allSingleSitemapsUrls[0]);
75
            }
76
        } elseif (preg_match('/<urlset/', $body)) {
77
            $this->validateBody($body, (string) $response->getUri());
78
        }
79
    }
80
}
81