Completed
Pull Request — master (#60)
by
unknown
05:01
created

SessionCheck   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 69
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A check() 0 17 2
A getCookie() 0 12 3
1
<?php
2
3
namespace SilverStripe\EnvironmentCheck\Checks;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Control\Controller;
7
use Psr\Http\Message\ResponseInterface;
8
use SilverStripe\Core\Config\Configurable;
9
use SilverStripe\EnvironmentCheck\Traits\Fetcher;
10
use SilverStripe\EnvironmentCheck\EnvironmentCheck;
11
12
/**
13
 * Check that a given URL does not generate a session.
14
 *
15
 * @author Adrian Humphreys
16
 * @package environmentcheck
17
 */
18
class SessionCheck implements EnvironmentCheck
19
{
20
    use Configurable;
21
    use Fetcher;
22
23
    /**
24
     * URL to check
25
     *
26
     * @var string
27
     */
28
    protected $url;
29
30
    /**
31
     * Set up check with URL
32
     *
33
     * @param string $url The route, excluding the domain
34
     * @inheritdoc
35
     */
36
    public function __construct($url = '')
37
    {
38
        $this->url = $url;
39
        $this->clientConfig = [
40
            'base_uri' => Director::absoluteBaseURL(),
41
            'timeout' => 10.0,
42
        ];
43
    }
44
45
    /**
46
     * Check that the response for URL does not create a session
47
     *
48
     * @return array
49
     */
50
    public function check(): array
51
    {
52
        $response = $this->fetchResponse($this->url);
53
        $cookie = $this->getCookie($response);
54
        $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url);
55
56
        if ($cookie) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cookie of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
57
            return [
58
                EnvironmentCheck::ERROR,
59
                "Sessions are being set for {$fullURL} : Set-Cookie => " . $cookie,
60
            ];
61
        }
62
        return [
63
            EnvironmentCheck::OK,
64
            "Sessions are not being created for {$fullURL} 👍",
65
        ];
66
    }
67
68
    /**
69
     * Get PHPSESSID or SECSESSID cookie set from the response if it exists.
70
     *
71
     * @param ResponseInterface $response
72
     * @return string|null Cookie contents or null if it doesn't exist
73
     */
74
    public function getCookie(ResponseInterface $response): ?string
75
    {
76
        $result = null;
77
        $cookies = $response->getHeader('Set-Cookie');
78
79
        foreach ($cookies as $cookie) {
80
            if (strpos($cookie, 'SESSID') !== false) {
81
                $result = $cookie;
82
            }
83
        }
84
        return $result;
85
    }
86
}
87