Completed
Pull Request — master (#31)
by
unknown
01:46
created

Filesystem::writeFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Yarak\Helpers;
4
5
use Yarak\Console\Output\Output;
6
use Yarak\Exceptions\WriteError;
7
8
trait Filesystem
9
{
10
    /**
11
     * Create all directories listed in directories array.
12
     * Return the number of created directories
13
     *
14
     * @param array  $directories
15
     * @param Output $output
16
     * @return  int
17
     */
18
    protected function makeDirectoryStructure(array $directories, Output $output = null)
19
    {
20
        $createdDirs = 0;
21
22
        foreach ($directories as $key => $directory) {
23
            if (!file_exists($directory)) {
24
                if (mkdir($directory)) {
25
                    $createdDirs++;
26
                }
27
28
                if ($output) {
29
                    $output->writeInfo("Created {$key} directory.");
30
                }
31
            }
32
        }
33
34
        return $createdDirs;
35
    }
36
37
    /**
38
     * Write contents to path.
39
     *
40
     * @param string $path
41
     * @param string $contents
42
     *
43
     * @throws WriteError
44
     */
45
    protected function writeFile($path, $contents)
46
    {
47
        try {
48
            file_put_contents($path, $contents);
49
        } catch (\Exception $e) {
50
            throw WriteError::fileWriteFailed($e, $path);
51
        }
52
    }
53
}
54