SymfonyFileSystem   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 39
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createTempFile() 0 7 1
A dumpToFile() 0 8 2
A createTempName() 0 4 1
A removeFile() 0 4 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