Completed
Push — master ( a6b26f...19dee2 )
by Andrii
03:13
created

FileHelper::mkdir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.2109

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 5
cts 8
cp 0.625
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2.2109
1
<?php
2
3
/*
4
 * Automation tool mixed with code generator for easier continuous development
5
 *
6
 * @link      https://github.com/hiqdev/hidev
7
 * @package   hidev
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\helpers;
13
14
use Yii;
15
16
/**
17
 * Hidev FileHelper.
18
 */
19
class FileHelper
20
{
21
    /**
22
     * Writes given content to the file.
23
     * Doesn't touch file if it has exactly same content.
24
     * Creates intermediate directories when necessary.
25
     * @param string $path
26
     * @param string $content
27
     * @return bool true if file was changed
28
     */
29 5
    public static function write($path, $content)
30
    {
31 5
        $path = Yii::getAlias($path);
32 5
        if (!is_file($path) || file_get_contents($path) !== $content) {
33 5
            static::mkdir(dirname($path));
34 5
            file_put_contents($path, $content);
35 5
            Yii::warning('Written file: ' . $path, 'file');
36
37 5
            return true;
38
        }
39
40
        return false;
41
    }
42
43
    /**
44
     * Creates directory if not exists.
45
     * Creates intermediate directories when necessary.
46
     * @param string $path
47
     * @return bool true if directory did not exist and was created
48
     */
49 5
    public static function mkdir($path)
50
    {
51 5
        $path = Yii::getAlias($path);
52 5
        $path = rtrim(trim($path), '/');
53 5
        if (!file_exists($path)) {
54
            mkdir($path, 0777, true);
55
            Yii::warning('Created dir:  ' . $path . '/', 'file');
56
57
            return true;
58
        }
59
60 5
        return false;
61
    }
62
}
63