CFileContent::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 16
ccs 0
cts 10
cp 0
crap 6
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Anax\Content;
4
5
/**
6
 * A read content from filesystem.
7
 *
8
 */
9
class CFileContent
10
{
11
    use \Anax\TConfigure,
12
        \Anax\DI\TInjectionAware;
13
14
15
16
    /**
17
     * Base path to content.
18
     *
19
     */
20
    private $path;
21
22
23
24
    /**
25
     * Get file as content.
26
     *
27
     * @param string $file with content
28
     *
29
     * @return string as content of the file
30
     *
31
     * @throws Exception when file does not exist
32
     */
33
    public function get($file)
34
    {
35
        $basepath = $this->config['basepath'];
36
        $suffix = $this->config['suffix'];
37
        $target = "$basepath/$file{$suffix}";
38
39
        if (!is_readable($target)) {
40
            throw new \Exception(t("No such file content: @FILENAME", ["@FILENAME" => $target]));
41
        }
42
43
        $filter  = $this->config['textfilter'];
44
        $content = file_get_contents($target);
45
        $content = $this->di->textFilter->doFilter($content, $filter);
46
47
        return $content;
48
    }
49
50
51
52
    /**
53
     * Set base path where  to find content.
54
     *
55
     * @param string $path where content reside
56
     *
57
     * @return $this
58
     */
59
    public function setBasePath($path)
60
    {
61
        if (!is_dir($path)) {
62
            throw new \Exception(t("Base path for file content is not a directory"));
63
        }
64
        $this->path = rtrim($path, '/') . '/';
65
66
        return $this;
67
    }
68
69
70
71
    /**
72
     * Set standard suffix.
73
     *
74
     * @param string $suffix to use as standard suffix of filename
75
     *
76
     * @return $this
77
     */
78
    public function setSuffix($suffix)
79
    {
80
        $this->config['suffix'] = $suffix;
81
82
        return $this;
83
    }
84
}
85