Skip to content

Commit 34bc262

Browse files
committed
Fix possible issue with getting the Session before it has been populated
1 parent cabefdb commit 34bc262

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Storage/LocalFileStorage.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ class LocalFileStorage implements FileStorageInterface
1818

1919
private SessionInterface $session;
2020

21-
public function __construct(RequestStack $requestStack, string $tempDir = null)
21+
public function __construct(private readonly RequestStack $requestStack, string $tempDir = null)
2222
{
23-
$this->session = $requestStack->getSession();
2423
$this->temporaryDirectory = $tempDir ?: sys_get_temp_dir() . DIRECTORY_SEPARATOR . 's2a' . DIRECTORY_SEPARATOR . 'collectionupload';
2524
}
2625

2726
public function storeFiles(array $files): array
2827
{
2928
$handledFiles = [];
30-
$sessionFiles = $this->session->get('s2a_collectionUpload_files', []);
29+
$sessionFiles = $this->getSession()->get('s2a_collectionUpload_files', []);
3130

3231
foreach ($files as $file) {
3332
$uid = uniqid();
@@ -48,7 +47,7 @@ public function storeFiles(array $files): array
4847
// TODO: should we add a limit to size of files in memory?
4948
$sessionFiles[$uid] = $fileDescriptor;
5049
}
51-
$this->session->set('s2a_collectionUpload_files', $sessionFiles);
50+
$this->getSession()->set('s2a_collectionUpload_files', $sessionFiles);
5251

5352
return $handledFiles;
5453
}
@@ -59,7 +58,7 @@ public function getFile(string $fileId = null): ?UploadedFile
5958
return null;
6059
}
6160

62-
$files = $this->session->get('s2a_collectionUpload_files', array());
61+
$files = $this->getSession()->get('s2a_collectionUpload_files', array());
6362
if (!array_key_exists($fileId, $files)) {
6463
return null;
6564
}
@@ -77,9 +76,14 @@ public function getFile(string $fileId = null): ?UploadedFile
7776
);
7877
}
7978
unset($files[$fileId]);
80-
$this->session->set('s2a_collectionUpload_files', $files);
79+
$this->getSession()->set('s2a_collectionUpload_files', $files);
8180

8281
return $file;
8382
}
8483

84+
private function getSession(): SessionInterface
85+
{
86+
return $this->session ??= $this->requestStack->getSession();
87+
}
88+
8589
}

0 commit comments

Comments
 (0)