FileManagerService::changeDir()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace AppBuilder\Application\Utils\FileManager;
6
7
use FileNotFoundException;
8
use Nette\DirectoryNotFoundException;
9
use Psr\Log\LoggerInterface;
10
use Symfony\Component\Filesystem\Exception\IOException;
11
use Symfony\Component\Filesystem\Filesystem;
12
13
class FileManagerService extends Filesystem implements FileManager
14
{
15
    /** @var LoggerInterface */
16
    private $logger;
17
18 8
    public function __construct(LoggerInterface $logger)
19
    {
20 8
        $this->logger = $logger;
21 8
    }
22
23
    /**
24
     * Uses Symfony Filesystem method to put data into file.
25
     * Creates file if it does not exist.
26
     *
27
     * @throws IOException
28
     */
29 3
    public function filePutContent(string $path, string $content) : bool
30
    {
31 3
        $this->dumpFile($path, $content);
32
33 2
        return true;
34
    }
35
36
    /**
37
     * Uses file_get_contents method to get data from file.
38
     *
39
     * @throws FileNotFoundException
40
     */
41 1
    public function fileGetContent(string $path) : string
42
    {
43 1
        $content = file_get_contents($path);
44 1
        if (null === $content) {
45
            throw new FileNotFoundException('Could not get content from file ' . $path);
46
        }
47
48 1
        return $content;
49
    }
50
51
    /**
52
     * Uses Symfony Filesystem method to remove file.
53
     */
54 1
    public function removeFile(string $path) : bool
55
    {
56
        try {
57 1
            $this->remove($path);
58
        } catch (IOException $exception) {
59
            $this->logger->warning($exception->getMessage(), [$exception]);
60
        }
61
62 1
        return true;
63
    }
64
65
    /**
66
     * Combines filepath from given params.
67
     */
68
    public function combineFilepath(string $homedir, string $ticketKey, string $filename) : string
69
    {
70
        return $homedir . $ticketKey . $filename;
71
    }
72
73
    /**
74
     * Checks wether passed file path is valid.
75
     */
76 2
    public function fileExists(string $path) : bool
77
    {
78 2
        return $this->exists($path);
79
    }
80
81
    /**
82
     * Creates directory from passed path.
83
     */
84 1
    public function createDir(string $path) : bool
85
    {
86
        try {
87 1
            $this->mkdir($path);
88
        } catch (IOException $exception) {
89
            $this->logger->warning($exception->getMessage(), [$exception]);
90
        }
91
92 1
        return true;
93
    }
94
95
    /**
96
     * Changes working directory to passed in param.
97
     *
98
     * @throws DirectoryNotFoundException
99
     */
100 1
    public function changeDir(string $path) : bool
101
    {
102 1
        if (!chdir($path)) {
103
            throw new DirectoryNotFoundException('Could not enter ' . $path);
104
        }
105
106 1
        return true;
107
    }
108
109
    /**
110
     * Uses Symfony Filesystem method to create symlinks.
111
     *
112
     * @throws IOException
113
     */
114
    public function createSymlink(string $source, string $target) : bool
115
    {
116
        $this->symlink($source, $target);
117
118
        return true;
119
    }
120
}
121