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

nepada / file-upload-control / 9738314049

01 Jul 2024 04:43AM UTC coverage: 90.293%. Remained the same
9738314049

Pull #158

github

web-flow
Merge bbbdee319 into cb835c645
Pull Request #158: Bump the phpstan group with 2 updates

679 of 752 relevant lines covered (90.29%)

0.9 hits per line

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

94.59
/src/FileUploadControl/Storage/Metadata/FileSystemMetadataJournal.php
1
<?php
2
declare(strict_types = 1);
3

4
namespace Nepada\FileUploadControl\Storage\Metadata;
5

6
use Nepada\FileUploadControl\Storage\FileUploadId;
7
use Nepada\FileUploadControl\Utils\FileSystem;
8
use Nepada\FileUploadControl\Utils\Finder;
9
use Nette;
10
use Nette\Utils\Json;
11

12
final class FileSystemMetadataJournal implements MetadataJournal
13
{
14

15
    use Nette\SmartObject;
16

17
    private const METADATA_FILE_SUFFIX = '.json';
18

19
    private FileSystem $fileSystem;
20

21
    private string $directory;
22

23
    private Finder $finder;
24

25
    public function __construct(FileSystem $fileSystem, Finder $finder, string $directory)
1✔
26
    {
27
        $this->fileSystem = $fileSystem;
1✔
28
        $this->finder = $finder;
1✔
29
        if (! $this->fileSystem->directoryExists($directory)) {
1✔
30
            throw new \InvalidArgumentException("Directory '$directory' does not exist.");
×
31
        }
32
        if (! $this->fileSystem->isWritable($directory)) {
1✔
33
            throw new \InvalidArgumentException("Directory '$directory' is not writable.");
×
34
        }
35
        $this->directory = $directory;
1✔
36
    }
1✔
37

38
    /**
39
     * @return FileUploadId[]
40
     */
41
    public function list(): array
42
    {
43
        /** @var \SplFileInfo[] $metadataFiles */
44
        $metadataFiles = [];
1✔
45
        foreach ($this->finder->findFilesInDirectory($this->directory, '*' . self::METADATA_FILE_SUFFIX) as $file) {
1✔
46
            $metadataFiles[] = $file;
1✔
47
        }
48
        usort(
1✔
49
            $metadataFiles,
1✔
50
            fn (\SplFileInfo $a, \SplFileInfo $b): int => $a->getMTime() <=> $b->getMTime(),
1✔
51
        );
52
        return array_map(
1✔
53
            fn (\SplFileInfo $file): FileUploadId => FileUploadId::fromString($file->getBasename(self::METADATA_FILE_SUFFIX)),
1✔
54
            $metadataFiles,
55
        );
56
    }
57

58
    /**
59
     * @throws FileUploadMetadataNotFoundException
60
     */
61
    public function load(FileUploadId $id): FileUploadMetadata
1✔
62
    {
63
        $file = $this->getFilePath($id);
1✔
64
        if (! $this->fileSystem->fileExists($file)) {
1✔
65
            throw FileUploadMetadataNotFoundException::withId($id);
1✔
66
        }
67
        $data = Json::decode($this->fileSystem->read($file), Json::FORCE_ARRAY);
1✔
68
        return FileUploadMetadata::fromArray($data);
1✔
69
    }
70

71
    /**
72
     * @throws FileUploadMetadataAlreadyExistsException
73
     */
74
    public function save(FileUploadId $id, FileUploadMetadata $metadata): void
1✔
75
    {
76
        $file = $this->getFilePath($id);
1✔
77
        if ($this->fileSystem->fileExists($file)) {
1✔
78
            throw FileUploadMetadataAlreadyExistsException::withId($id);
1✔
79
        }
80
        $data = $metadata->toArray();
1✔
81
        $this->fileSystem->write($file, Json::encode($data));
1✔
82
    }
1✔
83

84
    public function delete(FileUploadId $id): void
1✔
85
    {
86
        $this->fileSystem->delete($this->getFilePath($id));
1✔
87
    }
1✔
88

89
    public function destroy(): void
90
    {
91
        $this->fileSystem->delete($this->directory);
1✔
92
    }
1✔
93

94
    private function getFilePath(FileUploadId $id): string
1✔
95
    {
96
        return $this->directory . DIRECTORY_SEPARATOR . $id->toString() . self::METADATA_FILE_SUFFIX;
1✔
97
    }
98

99
}
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

© 2025 Coveralls, Inc