Completed
Branch master (b295a9)
by Marek
04:12 queued 01:32
created

FileManagerService::combineFilepath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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