Completed
Push — master ( b3febe...348067 )
by Zach
04:06 queued 02:03
created

Filesystem   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 33
rs 10
wmc 5
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A makeDirectoryStructure() 0 8 3
A writeFile() 0 8 2
1
<?php
2
3
namespace Yarak\Helpers;
4
5
use Yarak\Exceptions\WriteError;
6
7
trait Filesystem
8
{
9
    /**
10
     * Create all directories listed in directories array.
11
     *
12
     * @param array $directories
13
     */
14
    protected function makeDirectoryStructure(array $directories)
15
    {
16
        foreach ($directories as $directory) {
17
            if (!file_exists($directory)) {
18
                mkdir($directory);
19
            }
20
        }
21
    }
22
23
    /**
24
     * Write contents to path.
25
     *
26
     * @param string $path
27
     * @param string $contents
28
     *
29
     * @throws WriteError
30
     */
31
    protected function writeFile($path, $contents)
32
    {
33
        try {
34
            file_put_contents($path, $contents);
35
        } catch (\Exception $e) {
36
            throw WriteError::fileWriteFailed($e, $path);
37
        }
38
    }
39
}
40