FileSystemAdapter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 83
Duplicated Lines 27.71 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 23
loc 83
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fileExists() 0 8 2
A deleteFile() 0 8 2
A createDir() 10 10 3
A readFile() 0 10 3
A writeFile() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Hgraca\Lock\Adapter\FileSystem;
4
5
use Hgraca\FileSystem\Exception\FileNotFoundException;
6
use Hgraca\FileSystem\Exception\InvalidPathException;
7
use Hgraca\FileSystem\Exception\PathAlreadyExistsException;
8
use Hgraca\FileSystem\FileSystemInterface;
9
use Hgraca\FileSystem\LocalFileSystem;
10
use Hgraca\Lock\Port\FileSystem\Exception\FileNotFoundException as LockFileNotFoundException;
11
use Hgraca\Lock\Port\FileSystem\Exception\InvalidPathException as LockInvalidPathException;
12
use Hgraca\Lock\Port\FileSystem\Exception\PathAlreadyExistsException as LockPathAlreadyExistsException;
13
use Hgraca\Lock\Port\FileSystem\FileSystemInterface as LockFileSystemInterface;
14
15
final class FileSystemAdapter implements LockFileSystemInterface
16
{
17
    /**
18
     * @var FileSystemInterface
19
     */
20
    private $fileSystem;
21
22 22
    public function __construct(FileSystemInterface $fileSystem = null)
23
    {
24 22
        $this->fileSystem = $fileSystem ?? new LocalFileSystem(LocalFileSystem::IDEMPOTENT);
25 22
    }
26
27
    /**
28
     * @throws LockInvalidPathException
29
     */
30 11
    public function fileExists(string $path): bool
31
    {
32
        try {
33 11
            return $this->fileSystem->fileExists($path);
34 1
        } catch (InvalidPathException $e) {
35 1
            throw new LockInvalidPathException('', 0, $e);
36
        }
37
    }
38
39
    /**
40
     * @throws LockInvalidPathException
41
     */
42 4
    public function deleteFile(string $path): bool
43
    {
44
        try {
45 4
            return $this->fileSystem->deleteFile($path);
46 1
        } catch (InvalidPathException $e) {
47 1
            throw new LockInvalidPathException('', 0, $e);
48
        }
49
    }
50
51
    /**
52
     * Creates a folder and all intermediate folders if they don't exist
53
     *
54
     * @throws LockInvalidPathException
55
     * @throws LockPathAlreadyExistsException
56
     */
57 8 View Code Duplication
    public function createDir(string $path): bool
58
    {
59
        try {
60 8
            return $this->fileSystem->createDir($path);
61 2
        } catch (InvalidPathException $e) {
62 1
            throw new LockInvalidPathException('', 0, $e);
63 1
        } catch (PathAlreadyExistsException $e) {
64 1
            throw new LockPathAlreadyExistsException('', 0, $e);
65
        }
66
    }
67
68
    /**
69
     * @throws LockFileNotFoundException
70
     * @throws LockInvalidPathException
71
     */
72 9
    public function readFile(string $path): string
73
    {
74
        try {
75 9
            return $this->fileSystem->readFile($path);
76 3
        } catch (InvalidPathException $e) {
77 1
            throw new LockInvalidPathException('', 0, $e);
78 2
        } catch (FileNotFoundException $e) {
79 1
            throw new LockFileNotFoundException('', 0, $e);
80
        }
81
    }
82
83
    /**
84
     * @throws LockPathAlreadyExistsException
85
     * @throws LockInvalidPathException
86
     */
87 8 View Code Duplication
    public function writeFile(string $path, string $content)
88
    {
89
        try {
90 8
            $this->fileSystem->writeFile($path, $content);
91 2
        } catch (InvalidPathException $e) {
92 1
            throw new LockInvalidPathException('', 0, $e);
93 1
        } catch (PathAlreadyExistsException $e) {
94 1
            throw new LockPathAlreadyExistsException('', 0, $e);
95
        }
96 6
    }
97
}
98