Completed
Push — master ( a9ea1a...62657b )
by Andrii
05:05
created

FileHelper::write()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 6.28

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
ccs 2
cts 7
cp 0.2857
rs 9.4285
cc 3
eloc 8
nc 2
nop 2
crap 6.28
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
        $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 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
        $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 5
        return false;
61
    }
62
}
63