SessionManagement::deleteSession()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Api;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Interfaces\SessionManagementInterface;
6
use Sunnysideup\UpgradeToSilverstripe4\Traits\Creator;
7
8
class SessionManagement implements SessionManagementInterface
9
{
10
    use Creator;
11
12
    protected $sessionFileLocation = '';
13
14
    public static function initSession(string $sessionFileLocation): SessionManagementInterface
15
    {
16
        $obj = self::create();
17
        $obj->setSessionFileLocation($sessionFileLocation);
0 ignored issues
show
Bug introduced by
It seems like setSessionFileLocation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

17
        $obj->/** @scrutinizer ignore-call */ 
18
              setSessionFileLocation($sessionFileLocation);
Loading history...
18
19
        return $obj;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $obj could return the type Sunnysideup\UpgradeToSilverstripe4\Traits\Creator which is incompatible with the type-hinted return Sunnysideup\UpgradeToSil...sionManagementInterface. Consider adding an additional type-check to rule them out.
Loading history...
20
    }
21
22
    public function setSessionFileLocation($sessionFileLocation): SessionManagementInterface
23
    {
24
        $this->sessionFileLocation = $sessionFileLocation;
25
        $link = $sessionFileLocation;
26
        $dir = dirname($link);
27
        if (! file_exists($dir)) {
28
            user_error('Could not find: ' . $dir);
29
        }
30
31
        return $this;
32
    }
33
34
    public function getSessionFileLocation(): string
35
    {
36
        return $this->sessionFileLocation;
37
    }
38
39
    public function deleteSession()
40
    {
41
        unlink($this->getSessionFileLocation());
42
    }
43
44
    public function getSessionValue(string $key): string
45
    {
46
        $session = $this->getSessionData();
47
        if (isset($session[$key])) {
48
            return $session[$key];
49
        }
50
        return '';
51
    }
52
53
    public function getSessionData(): array
54
    {
55
        if (file_exists($this->getSessionFileLocation())) {
56
            $data = file_get_contents($this->getSessionFileLocation()) ?? '{}';
57
            if (! $data) {
58
                user_error('Could not read from: ' . $this->getSessionFileLocation());
59
            }
60
61
            return json_decode($data, true);
62
        }
63
        $this->setSessionData([]);
64
65
        return $this->getSessionData();
66
    }
67
68
    public function setSessionData(array $session): SessionManagementInterface
69
    {
70
        if (! file_exists($this->getSessionFileLocation())) {
71
            $session['Started'] = date('Y-m-d h:i ');
72
        }
73
        $data = json_encode($session, JSON_PRETTY_PRINT);
74
        try {
75
            $file = fopen($this->getSessionFileLocation(), 'w');
76
            if ($file === false) {
77
                throw new \RuntimeException('Failed to open file: ' . $this->getSessionFileLocation());
78
            }
79
            $writeOutcome = fwrite($file, (string) $data);
80
            if ($writeOutcome === false) {
81
                throw new \RuntimeException('Failed to write file: ' . $this->getSessionFileLocation());
82
            }
83
            $closeOutcome = fclose($file);
84
            if ($closeOutcome === false) {
85
                throw new \RuntimeException('Failed to close file: ' . $this->getSessionFileLocation());
86
            }
87
        } catch (\Exception $e) {
88
            // send error message if you can
89
            echo 'Caught exception: ' . $e->getMessage() . ' location of file: ' . $this->getSessionFileLocation();
90
        }
91
92
        return $this;
93
    }
94
95
    public function setSessionValue(string $key, string $value): SessionManagementInterface
96
    {
97
        $session = $this->getSessionData();
98
        $session[$key] = trim($value);
99
        $this->setSessionData($session);
100
101
        return $this;
102
    }
103
}
104