Completed
Push — master ( 23e70a...a8735e )
by Mikael
06:05
created

CFileContent::setSuffix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 2
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 View Code Duplication
    public function setBasePath($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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