Completed
Push — master ( 2977a7...5955f6 )
by Andrii
03:40
created

FileHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 44
ccs 4
cts 12
cp 0.3333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 13 3
A mkdir() 0 13 2
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