Completed
Push — master ( 092c93...6df846 )
by Adam
04:10
created

File   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A saveStringToFile() 0 19 3
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
     * Saves string to file.
20
     *
21
     * @param string $string        String to save
22
     * @param string $filename      Output filename without extension
23
     * @param string $fileExtension File extension. Default is '.dat'
24
     *
25
     * @see Generate::generateRandomNumbers()
26
     */
27
    public static function saveStringToFile($string, $filename, $fileExtension = '.dat')
28
    {
29
        // Create dir if not exists
30
        if (!is_dir(dirname($filename))) {
31
            Text::debug('Creating missing directory: '.dirname($filename));
32
            mkdir(dirname($filename));
33
        }
34
35
        // Warn about overwriting file
36
        if (file_exists($filename.$fileExtension)) {
37
            Text::message('File '.$filename.$fileExtension.' exists and it will be overwritten!');
38
            Input::dieOnDenyUserConfirm();
39
        }
40
41
        Text::message('Saving to file...');
42
43
        $outputFileBytes = file_put_contents($filename.$fileExtension, $string, LOCK_EX);
44
45
        Text::message('Output file '.$filename.$fileExtension.' generated with '.$outputFileBytes.' bytes.');
46
    }
47
}
48