Passed
Push — main ( 8f50ec...27cabb )
by Marco Aurélio
12:09 queued 08:15
created

SessionRetriever   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 44%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 24
c 1
b 0
f 0
dl 0
loc 56
ccs 11
cts 25
cp 0.44
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fake() 0 7 1
A insecure() 0 5 1
A retrieve() 0 24 2
1
<?php declare(strict_types=1);
2
3
namespace CustomerGauge\Session;
4
5
final class SessionRetriever
6
{
7
    private $path;
8
9
    private $domain;
10
11
    private $secure = true;
12
13
    private $session = null;
14
15 2
    public function __construct(string $path, string $domain)
16
    {
17 2
        $this->path = $path;
18 2
        $this->domain = $domain;
19 2
    }
20
21 2
    public static function fake(array $session): self
22
    {
23 2
        $instance = new self('', '');
24
25 2
        $instance->session = $session;
26
27 2
        return $instance;
28
    }
29
30
    public function insecure(): self
31
    {
32
        $this->secure = false;
33
34
        return $this;
35
    }
36
37 2
    public function retrieve(): array
38
    {
39 2
        if ($this->session !== null) {
40 2
            return $this->session;
41
        }
42
43
        ini_set('session.gc_maxlifetime', '1800');
44
        ini_set('session.save_handler', 'redis');
45
        ini_set('session.save_path', $this->path);
46
        ini_set('session.cookie_domain', $this->domain);
47
        ini_set('session.cookie_secure', (string) $this->secure);
48
        ini_set('session.cookie_httponly', '1');
49
50
        // When AWS Elasticache DNS resolution fails, PHP throws an error
51
        // session_start(): php_network_getaddresses: getaddrinfo failed: Name or service not known
52
        // This error is happening on 0.02% of our requests and AWS treat it as transient network
53
        // issues. Retrying before giving up might mitigate the problem.
54
        retry(3, function () {
55
            session_start();
56
        });
57
58
        $this->session = $_SESSION;
59
60
        return $this->session;
61
    }
62
}
63