• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 9045933531

11 May 2024 07:25PM UTC coverage: 86.566% (+0.3%) from 86.24%
9045933531

Pull #240

github

web-flow
Merge ef4f1596e into d6a8ae2f5
Pull Request #240: feat: Adds Timebox utility class and extends Clock

30 of 33 new or added lines in 4 files covered. (90.91%)

21 existing lines in 4 files now uncovered.

1321 of 1526 relevant lines covered (86.57%)

10.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.73
/src/Http/Session/Managers/FileSessionManager.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Http\Session\Managers;
6

7
use Tempest\Clock\Clock;
8
use function Tempest\event;
9
use Tempest\Http\Session\Session;
10
use Tempest\Http\Session\SessionConfig;
11
use Tempest\Http\Session\SessionDestroyed;
12
use Tempest\Http\Session\SessionId;
13
use Tempest\Http\Session\SessionManager;
14
use function Tempest\path;
15
use Throwable;
16

17
final readonly class FileSessionManager implements SessionManager
18
{
19
    public function __construct(
20
        private Clock $clock,
21
        private SessionConfig $sessionConfig,
22
    ) {
23
    }
11✔
24

25
    public function create(SessionId $id): Session
26
    {
27
        return $this->persist($id);
10✔
28
    }
29

30
    public function set(SessionId $id, string $key, mixed $value): void
31
    {
32
        $this->persist($id, [...$this->getData($id), ...[$key => $value]]);
7✔
33
    }
34

35
    public function get(SessionId $id, string $key, mixed $default = null): mixed
36
    {
37
        return $this->getData($id)[$key] ?? $default;
6✔
38
    }
39

40
    public function remove(SessionId $id, string $key): void
41
    {
42
        $data = $this->getData($id);
3✔
43

44
        unset($data[$key]);
3✔
45

46
        $this->persist($id, $data);
3✔
47
    }
48

49
    public function destroy(SessionId $id): void
50
    {
51
        unlink($this->getPath($id));
2✔
52

53
        event(new SessionDestroyed($id));
2✔
54
    }
55

56
    public function isValid(SessionId $id): bool
57
    {
58
        $session = $this->resolve($id);
2✔
59

60
        if ($session === null) {
2✔
61
            return false;
1✔
62
        }
63

64
        $validUntil = $session->createdAt->getTimestamp() + $this->sessionConfig->expirationInSeconds;
2✔
65

66
        if ($validUntil - $this->clock->time() <= 0) {
2✔
67
            return false;
2✔
68
        }
69

70
        return true;
2✔
71
    }
72

73
    private function getPath(SessionId $id): string
74
    {
75
        return path($this->sessionConfig->path, (string) $id);
11✔
76
    }
77

78
    private function resolve(SessionId $id): ?Session
79
    {
80
        $path = $this->getPath($id);
11✔
81

82
        try {
83
            $content = @file_get_contents($path);
11✔
84

85
            return unserialize($content);
11✔
86
        } catch (Throwable) {
10✔
87
            return null;
10✔
88
        }
89
    }
90

91
    /**
92
     * @param SessionId $id
93
     * @return array<mixed>
94
     */
95
    private function getData(SessionId $id): array
96
    {
97
        return $this->resolve($id)->data ?? [];
7✔
98
    }
99

100
    /**
101
     * @param SessionId $id
102
     * @param array<mixed>|null $data
103
     * @return Session
104
     */
105
    private function persist(SessionId $id, ?array $data = null): Session
106
    {
107
        $session = $this->resolve($id) ?? new Session(
11✔
108
            id: $id,
11✔
109
            createdAt: $this->clock->now(),
11✔
110
        );
11✔
111

112
        $path = $this->getPath($id);
11✔
113
        $dir = dirname($path);
11✔
114

115
        if (! is_dir($dir)) {
11✔
116
            mkdir($dir, recursive: true);
7✔
117
        }
118

119
        if ($data !== null) {
11✔
120
            $session->data = $data;
7✔
121
        }
122

123
        file_put_contents($path, serialize($session));
11✔
124

125
        return $session;
11✔
126
    }
127

128
    public function cleanup(): void
129
    {
130
        $sessionFiles = glob(path($this->sessionConfig->path, '/*'));
1✔
131

132
        foreach ($sessionFiles as $sessionFile) {
1✔
133
            $id = new SessionId(pathinfo($sessionFile, PATHINFO_FILENAME));
1✔
134

135
            $session = $this->resolve($id);
1✔
136

137
            if (! $session) {
1✔
UNCOV
138
                continue;
×
139
            }
140

141
            if ($this->isValid($session->id)) {
1✔
142
                continue;
1✔
143
            }
144

145
            $session->destroy();
1✔
146
        }
147
    }
148
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc