UnreachablePath::checkPaths()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Mage Scan
4
 *
5
 * PHP version 5
6
 *
7
 * @category  MageScan
8
 * @package   MageScan
9
 * @author    Steve Robbins <[email protected]>
10
 * @copyright 2015 Steve Robbins
11
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
12
 * @link      https://github.com/steverobbins/magescan
13
 */
14
15
namespace MageScan\Check;
16
17
use MageScan\File;
18
use GuzzleHttp\Psr7\Response;
19
20
/**
21
 * Checks that files/folder aren't accessible
22
 *
23
 * @category  MageScan
24
 * @package   MageScan
25
 * @author    Steve Robbins <[email protected]>
26
 * @copyright 2015 Steve Robbins
27
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
28
 * @link      https://github.com/steverobbins/magescan
29
 */
30
class UnreachablePath extends AbstractCheck
31
{
32
    /**
33
     * Get all paths to be tested
34
     *
35
     * @return string[]
36
     */
37
    public function getPaths()
38
    {
39
        $file = new File('unreachablepath.json');
40
        return $file->getJson();
41
    }
42
43
    /**
44
     * Test that paths are inaccessible
45
     *
46
     * @return array
47
     */
48
    public function checkPaths()
49
    {
50
        $result = [];
51
        $responses = $this->getRequest()->headMany($this->getPaths(), ['allow_redirects' => false]);
52
        foreach ($responses as $path => $response) {
53
            $result[] = $this->prepareResponse($path, $response);
54
        }
55
        return $result;
56
    }
57
58
    /**
59
     * Test that a path is inaccessible
60
     *
61
     * @param string $path
62
     *
63
     * @return array
64
     */
65
    public function checkPath($path)
66
    {
67
        $response = $this->getRequest()->head($path, ['allow_redirects' => false]);
68
        return $this->prepareResponse($path, $response);
69
    }
70
71
    /**
72
     * Build response array
73
     *
74
     * @param string   $path
75
     * @param Response $response
76
     *
77
     * @return string[]
78
     */
79
    protected function prepareResponse($path, Response $response)
80
    {
81
        return [
82
            $path,
83
            $response->getStatusCode(),
84
            $this->getUnreachableStatus($response)
85
        ];
86
    }
87
88
    /**
89
     * Get the status string for the given response
90
     *
91
     * @param Response $response
92
     *
93
     * @return mixed
94
     */
95
    protected function getUnreachableStatus(Response $response)
96
    {
97
        switch ($response->getStatusCode()) {
98
            case 200:
99
                return false;
100
            case 301:
101
            case 302:
102
                $headers = $response->getHeaders();
103
                return $headers['Location'][0];
104
        }
105
        return true;
106
    }
107
}
108