Filesystem   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
eloc 18
c 1
b 0
f 1
dl 0
loc 94
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A mkdir() 0 5 1
A readFromFile() 0 9 2
A writeToFile() 0 7 2
A __construct() 0 3 1
A exists() 0 3 1
A appendToFile() 0 7 2
A remove() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\FilesystemSymfony;
6
7
use Jellyfish\Filesystem\FilesystemInterface;
8
use Symfony\Component\Filesystem\Exception\IOException;
9
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
10
11
class Filesystem implements FilesystemInterface
12
{
13
    /**
14
     * @var \Symfony\Component\Filesystem\Filesystem
15
     */
16
    protected $symfonyFilesystem;
17
18
    /**
19
     * @param \Symfony\Component\Filesystem\Filesystem $symfonyFilesystem
20
     */
21
    public function __construct(SymfonyFilesystem $symfonyFilesystem)
22
    {
23
        $this->symfonyFilesystem = $symfonyFilesystem;
24
    }
25
26
    /**
27
     * @param string $path
28
     * @param int $mode
29
     *
30
     * @return \Jellyfish\Filesystem\FilesystemInterface
31
     */
32
    public function mkdir(string $path, int $mode = 0777): FilesystemInterface
33
    {
34
        $this->symfonyFilesystem->mkdir($path, $mode);
35
36
        return $this;
37
    }
38
39
    /**
40
     * @param string $path
41
     *
42
     * @return \Jellyfish\Filesystem\FilesystemInterface
43
     */
44
    public function remove(string $path): FilesystemInterface
45
    {
46
        $this->symfonyFilesystem->remove($path);
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param string $path
53
     *
54
     * @return bool
55
     */
56
    public function exists(string $path): bool
57
    {
58
        return $this->symfonyFilesystem->exists($path);
59
    }
60
61
    /**
62
     * @param string $pathToFile
63
     * @param string $content
64
     *
65
     * @return \Jellyfish\Filesystem\FilesystemInterface
66
     */
67
    public function appendToFile(string $pathToFile, string $content): FilesystemInterface
68
    {
69
        if (false === @\file_put_contents($pathToFile, $content, FILE_APPEND)) {
70
            throw new IOException(sprintf('Failed to write file "%s".', $pathToFile), 0, null, $pathToFile);
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * @param string $pathToFile
78
     * @param string $content
79
     *
80
     * @return \Jellyfish\Filesystem\FilesystemInterface
81
     */
82
    public function writeToFile(string $pathToFile, string $content): FilesystemInterface
83
    {
84
        if (false === @\file_put_contents($pathToFile, $content)) {
85
            throw new IOException(sprintf('Failed to write file "%s".', $pathToFile), 0, null, $pathToFile);
86
        }
87
88
        return $this;
89
    }
90
91
    /**
92
     * @param string $pathToFile
93
     *
94
     * @return string
95
     */
96
    public function readFromFile(string $pathToFile): string
97
    {
98
        $fileContent = @\file_get_contents($pathToFile);
99
100
        if (false === $fileContent) {
101
            throw new IOException(sprintf('Failed to read file "%s".', $pathToFile), 0, null, $pathToFile);
102
        }
103
104
        return $fileContent;
105
    }
106
}
107