Passed
Push — master ( f796f4...f2d884 )
by Adam
02:53 queued 01:13
created

File::createMissingDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Includes/File.php.
4
 *
5
 * @author  Adam "Saibamen" Stachowicz <[email protected]>
6
 * @license MIT
7
 *
8
 * @link    https://github.com/Saibamen/Generate-Sort-Numbers
9
 */
10
11
namespace Includes;
12
13
/**
14
 * Functions for operating on files.
15
 */
16
class File
17
{
18
    /**
19
     * Create dir from filename if not exists.
20
     *
21
     * @param string $filename Filename
22
     */
23
    public static function createMissingDirectory($filename)
24
    {
25
        if (!is_dir(dirname($filename))) {
26
            Text::debug('Creating missing directory: '.dirname($filename));
27
            mkdir(dirname($filename));
28
        }
29
    }
30
31
    /**
32
     * Warn about overwriting file if exists and empty it.
33
     *
34
     * @param string $filename      Filename without extension
35
     * @param string $fileExtension File extension. Default is '.dat'
36
     *
37
     * @see Input::dieOnDenyUserConfirm()
38
     */
39
    public static function checkIfFileExists($filename, $fileExtension = '.dat')
40
    {
41
        if (file_exists($filename.$fileExtension)) {
42
            Text::message('File '.$filename.$fileExtension.' exists and it will be overwritten!');
43
44
            Input::dieOnDenyUserConfirm();
45
46
            // Empty the file
47
            file_put_contents($filename.$fileExtension, '');
48
        }
49
    }
50
}
51