Filesystem::dumpFile()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 16
cp 0
rs 8.8337
c 0
b 0
f 0
cc 6
nc 9
nop 3
crap 42
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP\Helper;
5
6
use Symfony\Component\Filesystem\Exception\IOException;
7
use Symfony\Component\Filesystem\Filesystem as BaseFilesystem;
8
9
/**
10
 * Provides basic utility to manipulate the file system.
11
 */
12
class Filesystem extends BaseFilesystem
13
{
14
    /**
15
     * Atomically dumps content into a file.
16
     *
17
     * @param  string      $filename The file to be written to.
18
     * @param  string      $content  The data to write into the file.
19
     * @param  int|null    $mode     The file mode (octal). If null, file permissions are not modified
20
     *                               Deprecated since version 2.3.12, to be removed in 3.0.
21
     *
22
     * @throws IOException If the file cannot be written to.
23
     */
24
    public function dumpFile($filename, $content, ?int $mode = 0666) : void
25
    {
26
        $dir = dirname($filename);
27
28
        if (! is_dir($dir)) {
29
            $this->mkdir($dir);
30
        } elseif (! is_writable($dir)) {
31
            throw new IOException(sprintf('Unable to write to the "%s" directory.', $dir), 0, null, $dir);
32
        }
33
34
        // "tempnam" did not work with VFSStream for tests
35
        $tmpFile = dirname($filename) . '/temp_' . md5(time() . basename($filename));
36
37
        if (false === @file_put_contents($tmpFile, $content)) {
38
            throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
39
        }
40
41
        try {
42
            $this->rename($tmpFile, $filename, true);
43
        } catch (IOException $e) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Filesystem\Exception\IOException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
44
            unlink($tmpFile);
45
46
            throw $e;
47
        }
48
49
        if (null !== $mode) {
50
            $this->chmod($filename, $mode);
51
        }
52
    }
53
}
54