Completed
Push — master ( 6695d4...9bc3b7 )
by Harry
04:41
created

MakeDirectory::makeDirectory()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

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