Completed
Push — master ( 128ddb...c5783a )
by
unknown
14s queued 10s
created

SessionCheck   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 15 2
A __construct() 0 3 1
A getCookie() 0 11 3
1
<?php
2
3
namespace SilverStripe\EnvironmentCheck\Checks;
4
5
use Psr\Http\Message\ResponseInterface;
6
use SilverStripe\EnvironmentCheck\Traits\Fetcher;
7
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
8
9
/**
10
 * Check that a given URL does not generate a session.
11
 *
12
 * @author Adrian Humphreys
13
 * @package environmentcheck
14
 */
15
class SessionCheck implements EnvironmentCheck
16
{
17
    use Fetcher;
18
19
    /**
20
     * Set up check with URL
21
     *
22
     * @param string $url The route, excluding the domain
23
     * @inheritdoc
24
     */
25
    public function __construct($url = '')
26
    {
27
        $this->setURL($url);
28
    }
29
30
    /**
31
     * Check that the response for URL does not create a session
32
     *
33
     * @return array
34
     */
35
    public function check()
36
    {
37
        $response = $this->client->get($this->getURL());
38
        $cookie = $this->getCookie($response);
39
        $fullURL = $this->getURL();
40
41
        if ($cookie) {
42
            return [
43
                EnvironmentCheck::ERROR,
44
                "Sessions are being set for {$fullURL} : Set-Cookie => " . $cookie,
45
            ];
46
        }
47
        return [
48
            EnvironmentCheck::OK,
49
            "Sessions are not being created for {$fullURL} 👍",
50
        ];
51
    }
52
53
    /**
54
     * Get PHPSESSID or SECSESSID cookie set from the response if it exists.
55
     *
56
     * @param ResponseInterface $response
57
     * @return string|null Cookie contents or null if it doesn't exist
58
     */
59
    public function getCookie(ResponseInterface $response)
60
    {
61
        $result = null;
62
        $cookies = $response->getHeader('Set-Cookie');
63
64
        foreach ($cookies as $cookie) {
65
            if (strpos($cookie, 'SESSID') !== false) {
66
                $result = $cookie;
67
            }
68
        }
69
        return $result;
70
    }
71
}
72