Completed
Push — master ( 9bc3b7...a15851 )
by Harry
02:46
created

MakeDirectory::makeDirectory()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
ccs 6
cts 8
cp 0.75
rs 9.2
cc 4
eloc 8
nc 3
nop 2
crap 4.25
1
<?php
2
3
namespace Graze\DataFile\Modify;
4
5
use Graze\DataFile\Modify\Exception\MakeDirectoryFailedException;
6
use Graze\DataFile\Node\FileNode;
7
use Graze\DataFile\Node\LocalFile;
8
9
class MakeDirectory
10
{
11
    const VISIBILITY_PUBLIC  = 'public';
12
    const VISIBILITY_PRIVATE = 'private';
13
14
    /**
15
     * Create the directory specified by the $file if it does not exist
16
     *
17
     * @param FileNode $file
18
     * @param string   $visibility public or private visibility
19
     *
20
     * @return LocalFile The original file inputted
21
     * @throws MakeDirectoryFailedException
22
     */
23 10
    public function makeDirectory(FileNode $file, $visibility = null)
24
    {
25 10
        if (!($file instanceof FileNode)) {
26
            throw new \InvalidArgumentException("Node: $file is not a FileNode");
27
        }
28
29 10
        $madeDirectory = $file->getFilesystem()->createDir($file->getDirectory(), [
30 10
            'visibility' => $visibility ?: static::VISIBILITY_PUBLIC,
31
        ]);
32 10
        if (!$madeDirectory) {
33
            throw new MakeDirectoryFailedException($file, error_get_last()['message']);
34
        }
35
36 10
        return $file;
37
    }
38
}
39