Completed
Pull Request — master (#121)
by Vladimir
02:51 queued 01:05
created

File   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
ccs 12
cts 14
cp 0.8571
wmc 6
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createFileForRelativePath() 0 4 1
A getBasename() 0 4 1
A getFilename() 0 4 1
A getContents() 0 17 3
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Filesystem;
9
10
use allejo\stakx\Service;
11
12
/**
13
 * A representation of a file on a given filesystem, virtual or physical.
14
 *
15
 * This class extends \SplFileInfo and adds new methods along with overriding some methods solely because I feel that
16
 * some of the naming can be misleading.
17
 *
18
 * @since 0.2.0
19
 */
20
final class File extends BaseFilesystemItem
21
{
22
    /**
23
     * Get a new File object for another file relative to this file.
24
     *
25
     * @param string $path
26
     *
27
     * @return File
28
     */
29 11
    public function createFileForRelativePath($path)
30
    {
31 11
        return new File(Service::getWorkingDirectory() . DIRECTORY_SEPARATOR . $path);
32
    }
33
34
    /**
35
     * Get the name of the file without an extension.
36
     *
37
     * @param null $suffix this value will be discarded and is only needed to be able to override the \SplFileInfo
38
     *                     definition
39
     *
40
     * @since 0.2.0
41
     *
42
     * @return string
43
     */
44 97
    public function getBasename($suffix = null)
45
    {
46 97
        return parent::getBasename('.' . $this->getExtension());
47
    }
48
49
    /**
50
     * Get the name of the with the extension.
51
     *
52
     * @since 0.2.0
53
     *
54
     * @return string
55
     */
56 101
    public function getFilename()
57
    {
58 101
        return $this->getFullName();
59
    }
60
61
    /**
62
     * Get the contents of this file.
63
     *
64
     * @since 0.2.0
65
     *
66
     * @throws \RuntimeException when the file could not be read
67
     *
68
     * @return string
69
     */
70 157
    public function getContents()
71
    {
72 157
        if (!$this->exists())
73
        {
74 1
            throw $this->buildNotFoundException();
75
        }
76
77 157
        $content = file_get_contents($this->getAbsolutePath());
78
79 157
        if ($content === false)
80
        {
81
            $error = error_get_last();
82
            throw new \RuntimeException($error['message']);
83
        }
84
85 157
        return $content;
86
    }
87
}
88