Passed
Push — master ( a3920c...49b830 )
by Sebastian
01:58
created

File::create()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 3
1
<?php
2
/**
3
 * This file is part of Camino.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianFeldmann\Camino\Path;
11
12
use RuntimeException;
13
14
/**
15
 * Class File
16
 *
17
 * @package SebastianFeldmann\Camino
18
 */
19
final class File extends Base
20
{
21
    /**
22
     * Returns a directory instance of its location
23
     *
24
     * @return \SebastianFeldmann\Camino\Path\Directory
25
     */
26 1
    public function getDirectory(): Directory
27
    {
28 1
        return new Directory(dirname($this->raw));
29
    }
30
31
    /**
32
     * Is the file located in a given directory
33
     *
34
     * @param  \SebastianFeldmann\Camino\Path\Directory $directory
35
     * @return bool
36
     */
37 2
    public function isInDirectory(Directory $directory): bool
38
    {
39 2
        return $this->isChildOf($directory);
40
    }
41
42
    /**
43
     * Factory method to create files that actually exist
44
     *
45
     * @param  string $path
46
     * @return \SebastianFeldmann\Camino\Path\File
47
     */
48 6
    public static function create(string $path): File
49
    {
50 6
        $realPath = realpath($path);
51 6
        if (empty($realPath) || is_dir($realPath)) {
52 4
            throw new RuntimeException('file does not exist: ' . $path);
53
        }
54 2
        return new self($realPath);
55
    }
56
}
57