FileSystemAdapter::writeFile()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Hgraca\Cache\PhpFile\Adapter\FileSystem;
4
5
use Hgraca\Cache\PhpFile\Port\FileSystem\Exception\FileNotFoundException as CacheFileNotFoundException;
6
use Hgraca\Cache\PhpFile\Port\FileSystem\Exception\InvalidPathException as CacheInvalidPathException;
7
use Hgraca\Cache\PhpFile\Port\FileSystem\Exception\PathAlreadyExistsException as CachePathAlreadyExistsException;
8
use Hgraca\Cache\PhpFile\Port\FileSystem\FileSystemInterface as CacheFileSystemInterface;
9
use Hgraca\FileSystem\Exception\FileNotFoundException;
10
use Hgraca\FileSystem\Exception\InvalidPathException;
11
use Hgraca\FileSystem\Exception\PathAlreadyExistsException;
12
use Hgraca\FileSystem\FileSystemInterface;
13
use Hgraca\FileSystem\LocalFileSystem;
14
15
final class FileSystemAdapter implements CacheFileSystemInterface
16
{
17
    /**
18
     * @var FileSystemInterface
19
     */
20
    private $fileSystem;
21
22 23
    public function __construct(FileSystemInterface $fileSystem = null)
23
    {
24 23
        $this->fileSystem = $fileSystem ?? new LocalFileSystem(LocalFileSystem::IDEMPOTENT);
25 23
    }
26
27
    /**
28
     * @throws CacheInvalidPathException
29
     */
30 13
    public function fileExists(string $path): bool
31
    {
32
        try {
33 13
            return $this->fileSystem->fileExists($path);
34 1
        } catch (InvalidPathException $e) {
35 1
            throw new CacheInvalidPathException('', 0, $e);
36
        }
37
    }
38
39
    /**
40
     * @throws CacheFileNotFoundException
41
     * @throws CacheInvalidPathException
42
     */
43 4
    public function readFile(string $path): string
44
    {
45
        try {
46 4
            return $this->fileSystem->readFile($path);
47 2
        } catch (InvalidPathException $e) {
48 1
            throw new CacheInvalidPathException('', 0, $e);
49 1
        } catch (FileNotFoundException $e) {
50 1
            throw new CacheFileNotFoundException('', 0, $e);
51
        }
52
    }
53
54
    /**
55
     * @throws CachePathAlreadyExistsException
56
     * @throws CacheInvalidPathException
57
     */
58 7
    public function writeFile(string $path, string $content)
59
    {
60
        try {
61 7
            $this->fileSystem->writeFile($path, $content);
62 2
        } catch (InvalidPathException $e) {
63 1
            throw new CacheInvalidPathException('', 0, $e);
64 1
        } catch (PathAlreadyExistsException $e) {
65 1
            throw new CachePathAlreadyExistsException('', 0, $e);
66
        }
67 5
    }
68
69
    /**
70
     * @throws CacheInvalidPathException
71
     */
72 3
    public function getFileCreationTimestamp(string $path): int
73
    {
74
        try {
75 3
            return $this->fileSystem->getFileCreationTimestamp($path);
76 1
        } catch (InvalidPathException $e) {
77 1
            throw new CacheInvalidPathException('', 0, $e);
78
        }
79
    }
80
}
81