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

tempestphp / tempest-framework / 14049246919

24 Mar 2025 09:42PM UTC coverage: 79.353% (-0.04%) from 79.391%
14049246919

push

github

web-flow
feat(support): support array parameters in string manipulations (#1073)

48 of 48 new or added lines in 2 files covered. (100.0%)

735 existing lines in 126 files now uncovered.

10492 of 13222 relevant lines covered (79.35%)

90.78 hits per line

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

91.38
/src/Tempest/Router/src/Session/Managers/FileSessionManager.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Router\Session\Managers;
6

7
use Tempest\Clock\Clock;
8
use Tempest\Router\Session\Session;
9
use Tempest\Router\Session\SessionConfig;
10
use Tempest\Router\Session\SessionDestroyed;
11
use Tempest\Router\Session\SessionId;
12
use Tempest\Router\Session\SessionManager;
13
use Throwable;
14

15
use function Tempest\event;
16
use function Tempest\internal_storage_path;
17

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

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

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

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

40
    public function remove(SessionId $id, string $key): void
3✔
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
4✔
50
    {
51
        unlink($this->getPath($id));
4✔
52

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

56
    public function isValid(SessionId $id): bool
2✔
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
        return ($validUntil - $this->clock->time()) > 0;
2✔
67
    }
68

69
    private function getPath(SessionId $id): string
45✔
70
    {
71
        return internal_storage_path($this->sessionConfig->path, (string) $id);
45✔
72
    }
73

74
    private function resolve(SessionId $id): ?Session
45✔
75
    {
76
        $path = $this->getPath($id);
45✔
77

78
        try {
79
            if (! is_file($path)) {
45✔
80
                return null;
44✔
81
            }
82

83
            $content = file_get_contents($path);
17✔
84

85
            return unserialize($content, ['allowed_classes' => true]);
17✔
UNCOV
86
        } catch (Throwable) {
×
87
            return null;
×
88
        }
89
    }
90

UNCOV
91
    public function all(SessionId $id): array
×
92
    {
UNCOV
93
        return $this->getData($id);
×
94
    }
95

96
    /**
97
     * @return array<mixed>
98
     */
99
    private function getData(SessionId $id): array
16✔
100
    {
101
        return $this->resolve($id)->data ?? [];
16✔
102
    }
103

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

114
        $path = $this->getPath($id);
45✔
115
        $dir = dirname($path);
45✔
116

117
        if (! is_dir($dir)) {
45✔
118
            mkdir($dir, recursive: true);
10✔
119
        }
120

121
        if ($data !== null) {
45✔
122
            $session->data = $data;
13✔
123
        }
124

125
        file_put_contents($path, serialize($session));
45✔
126

127
        return $session;
45✔
128
    }
129

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

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

137
            $session = $this->resolve($id);
1✔
138

139
            if ($session === null) {
1✔
UNCOV
140
                continue;
×
141
            }
142

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

147
            $session->destroy();
1✔
148
        }
149
    }
150
}
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