FileReader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
namespace Balloon\Reader;
3
4
/**
5
 * Class FileReader
6
 * @package Balloon\Reader
7
 * @author Raphaël Lefebvre <[email protected]>
8
 */
9
class FileReader implements IFileReader
10
{
11
    /**
12
     * @var string
13
     */
14
    private $filePath;
15
16
    /**
17
     * @var bool
18
     */
19
    private $useIncludePath;
20
21
    /**
22
     * @var resource
23
     */
24
    private $context;
25
26
    /**
27
     * @var bool
28
     */
29
    private $exists = false;
30
31
    /**
32
     * @param string $filePath
33
     * @param bool $useIncludePath
34
     * @param resource $context
0 ignored issues
show
Documentation introduced by
Should the type for parameter $context not be resource|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
35
     */
36
    public function __construct($filePath, $useIncludePath = false, $context = null)
37
    {
38
        $this->setFilePath($filePath);
39
        $this->setUseIncludePath($useIncludePath);
40
        $this->setContext($context);
0 ignored issues
show
Bug introduced by
It seems like $context defined by parameter $context on line 36 can also be of type null; however, Balloon\Reader\FileReader::setContext() does only seem to accept resource, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function read()
47
    {
48
        if(!$this->isExistingFile()){
49
            return '';
50
        }
51
        return file_get_contents($this->getFilePath(), $this->useIncludePath(), $this->getContext());
52
    }
53
54
    /**
55
     * @param string $data
56
     * @param int $mode
57
     * @return int
58
     */
59
    public function write($data, $mode = 0)
60
    {
61
        $this->createDir();
62
        return file_put_contents($this->getFilePath(), $data, $mode, $this->getContext());
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function isExistingFile()
69
    {
70
        if (!$this->exists) {
71
            $this->exists = file_exists($this->getFilePath());
72
        }
73
        return $this->exists;
74
    }
75
76
    /**
77
     * @return bool
78
     */
79
    public function isExistingDir()
80
    {
81
        if (!$this->exists) {
82
            $this->exists = is_dir($this->getDir());
83
        }
84
        return $this->exists;
85
    }
86
87
    /**
88
     * Getter of $filePath
89
     *
90
     * @return string
91
     */
92
    public function getFilePath()
93
    {
94
        return $this->filePath;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getDir()
101
    {
102
        return dirname($this->getFilePath());
103
    }
104
105
    /**
106
     * Getter of $useIncludePath
107
     *
108
     * @return boolean
109
     */
110
    public function useIncludePath()
111
    {
112
        return $this->useIncludePath;
113
    }
114
115
    /**
116
     * Getter of $context
117
     *
118
     * @return resource
119
     */
120
    public function getContext()
121
    {
122
        return $this->context;
123
    }
124
125
    /**
126
     * Setter of $filePath
127
     *
128
     * @param string $filePath
129
     */
130
    private function setFilePath($filePath)
131
    {
132
        $this->filePath = (string)$filePath;
133
    }
134
135
    /**
136
     * Setter of $useIncludePath
137
     *
138
     * @param boolean $useIncludePath
139
     */
140
    private function setUseIncludePath($useIncludePath)
141
    {
142
        $this->useIncludePath = (boolean)$useIncludePath;
143
    }
144
145
    /**
146
     * Setter of $context
147
     *
148
     * @param resource $context
149
     */
150
    private function setContext($context)
151
    {
152
        $this->context = $context;
153
    }
154
155
    /**
156
     *
157
     */
158
    private function createDir()
159
    {
160
        if (!$this->isExistingDir()) {
161
            mkdir($this->getDir(), 0777, true);
162
            $this->exists = true;
163
        }
164
    }
165
}
166
167