SymfonyFileSystem::createTempName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace MyBuilder\Cronos\Updater;
4
5
use Symfony\Component\Filesystem\Filesystem as FileSystemHelper;
6
7
class SymfonyFileSystem implements FileSystem
8
{
9
    /** @var FileSystemHelper; */
10
    private $filesystem;
11
12
    public function __construct()
13
    {
14
        $this->filesystem = new FileSystemHelper();
15
    }
16
17
    /** @throws \Symfony\Component\Filesystem\Exception\IOException If the file cannot be written to. */
18
    public function createTempFile($prefix, $content): string
19
    {
20
        $filePath = $this->createTempName($prefix);
21
        $this->dumpToFile($filePath, $content);
22
23
        return $filePath;
24
    }
25
26
    private function dumpToFile(string $filePath, string $content): void
27
    {
28
        if (\method_exists($this->filesystem, 'dumpFile')) {
29
            $this->filesystem->dumpFile($filePath, $content);
30
        } else {
31
            \file_put_contents($filePath, $content);
32
        }
33
    }
34
35
    private function createTempName(string $prefix): string
36
    {
37
        return \tempnam(\sys_get_temp_dir(), $prefix);
38
    }
39
40
    /** @throws \Symfony\Component\Filesystem\Exception\IOException When removal fails */
41
    public function removeFile(string $filePath): void
42
    {
43
        $this->filesystem->remove($filePath);
44
    }
45
}
46