Completed
Push — master ( 62f786...e41aa3 )
by Andrii
02:37
created

FileHelper::mkdir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.864

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 2
cts 5
cp 0.4
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2.864
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 3
    public static function write($path, $content)
30
    {
31
        $path = Yii::getAlias($path);
32
        if (!is_file($path) || file_get_contents($path) !== $content) {
33
            static::mkdir(dirname($path));
34
            file_put_contents($path, $content);
35
            Yii::warning('Written file: ' . $path, 'file');
36
37 3
            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 3
    public static function mkdir($path)
50
    {
51
        $path = Yii::getAlias($path);
52
        $path = rtrim(trim($path), '/');
53
        if (!file_exists($path)) {
54
            mkdir($path, 0777, true);
55
            Yii::warning('Created dir:  ' . $path . '/', 'file');
56
57
            return true;
58
        }
59
60 3
        return false;
61
    }
62
}
63