File::exists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of Payload.
5
 *
6
 * (c) DraperStudio <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DraperStudio\Payload\Utils;
13
14
use DraperStudio\Payload\Exceptions\FileDoesNotExistException;
15
16
/**
17
 * Class File.
18
 */
19
class File
20
{
21
    /**
22
     * @param $path
23
     *
24
     * @return mixed
25
     */
26 45
    public static function extension($path)
27
    {
28 45
        return pathinfo($path, PATHINFO_EXTENSION);
29
    }
30
31
    /**
32
     * @param $path
33
     *
34
     * @return bool
35
     */
36 39
    public static function exists($path)
37
    {
38 39
        return file_exists($path);
39
    }
40
41
    /**
42
     * @param $path
43
     *
44
     * @return string
45
     *
46
     * @throws FileDoesNotExistException
47
     */
48 24
    public static function contents($path)
49
    {
50 24
        if (self::exists($path)) {
51 21
            return trim(file_get_contents($path));
52
        }
53
54 3
        throw new FileDoesNotExistException(sprintf('%s is not a valid file', $path));
55
    }
56
57
    /**
58
     * @param $path
59
     *
60
     * @return mixed
61
     *
62
     * @throws FileDoesNotExistException
63
     */
64 9
    public static function get($path)
65
    {
66 9
        if (self::exists($path)) {
67 6
            return require $path;
68
        }
69
70 3
        throw new FileDoesNotExistException(sprintf('%s is not a valid file', $path));
71
    }
72
73
    /**
74
     * @param $path
75
     * @param $contents
76
     *
77
     * @return bool
78
     */
79 24
    public static function put($path, $contents)
80
    {
81 24
        return (bool) file_put_contents($path, $contents);
82
    }
83
}
84