Completed
Pull Request — master (#186)
by Jay
05:38
created

Filesystem   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 41
ccs 10
cts 16
cp 0.625
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B dumpFile() 0 29 6
1
<?php
2
/**
3
 * Copyright (c) 1998-2015 Browser Capabilities Project
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included
13
 * in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category   Browscap-PHP
24
 * @copyright  1998-2015 Browser Capabilities Project
25
 * @license    http://www.opensource.org/licenses/MIT MIT License
26
 * @link       https://github.com/browscap/browscap-php/
27
 * @since      added with version 3.0
28
 */
29
30
namespace BrowscapPHP\Helper;
31
32
use Symfony\Component\Filesystem\Exception\IOException;
33
use Symfony\Component\Filesystem\Filesystem as BaseFilesystem;
34
35
/**
36
 * Provides basic utility to manipulate the file system.
37
 *
38
 * @author Fabien Potencier <[email protected]>
39
 */
40
class Filesystem extends BaseFilesystem
41
{
42
    /**
43
     * Atomically dumps content into a file.
44
     *
45
     * @param  string      $filename The file to be written to.
46
     * @param  string      $content  The data to write into the file.
47
     * @param  int         $mode     The file mode (octal). If null, file permissions are not modified
48
     *                               Deprecated since version 2.3.12, to be removed in 3.0.
49
     * @throws IOException If the file cannot be written to.
50
     */
51 2
    public function dumpFile($filename, $content, $mode = 0666)
52
    {
53 2
        $dir = dirname($filename);
54
55 2
        if (!is_dir($dir)) {
56
            $this->mkdir($dir);
57 2
        } elseif (!is_writable($dir)) {
58
            throw new IOException(sprintf('Unable to write to the "%s" directory.', $dir), 0, null, $dir);
59
        }
60
61
        // "tempnam" did not work with VFSStream for tests
62 2
        $tmpFile = dirname($filename) . '/temp_' . md5(time() . basename($filename));
63
64 2
        if (false === @file_put_contents($tmpFile, $content)) {
65
            throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
66
        }
67
68
        try {
69 2
            $this->rename($tmpFile, $filename, true);
70
        } catch (IOException $e) {
71
            unlink($tmpFile);
72
73
            throw $e;
74
        }
75
76 2
        if (null !== $mode) {
77 2
            $this->chmod($filename, $mode);
78
        }
79 2
    }
80
}
81