FileReaderProxy::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace Balloon\Proxy;
3
4
use Balloon\Reader\IFileReader;
5
6
/**
7
 * Class FileReaderProxy
8
 * @package Balloon\Proxy
9
 * @author Raphaël Lefebvre <[email protected]>
10
 */
11
class FileReaderProxy implements IFileReader, IProxy
12
{
13
    /**
14
     * @var IFileReader
15
     */
16
    private $fileReader;
17
18
    /**
19
     * @var FileReaderCache
20
     */
21
    private $cache;
22
23
    /**
24
     * @var bool
25
     */
26
    private $isCached = false;
27
28
    /**
29
     * @var bool
30
     */
31
    private $hasChanged = false;
32
33
    /**
34
     * @param IFileReader $fileReader
35
     * @param FileReaderCache $cache
36
     */
37
    public function __construct(IFileReader $fileReader, FileReaderCache $cache)
38
    {
39
        $this->fileReader = $fileReader;
40
        $this->cache = $cache;
41
    }
42
43
    /**
44
     * @return mixed
45
     */
46
    public function read()
47
    {
48
        if(!$this->isCached){
49
            $this->cache->write($this->fileReader->read());
50
            $this->isCached = true;
51
        }
52
        return $this->cache->read();
53
    }
54
55
    /**
56
     * @param mixed $data
57
     * @param int $mode
58
     * @return int
59
     */
60
    public function write($data, $mode = 0)
61
    {
62
        $this->hasChanged = true;
63
        return $this->cache->write($data, $mode);
64
    }
65
66
    /**
67
     * @return int
68
     */
69
    public function flush()
70
    {
71
        if($this->hasChanged){
72
            $this->hasChanged = false;
73
            return $this->fileReader->write($this->cache->read());
74
        }
75
        return 0;
76
    }
77
78
    /**
79
     *
80
     */
81
    public function clear()
82
    {
83
        $this->invalidate();
84
        $this->read();
85
    }
86
87
    /**
88
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
89
     */
90
    public function invalidate()
91
    {
92
        $this->isCached = false;
93
    }
94
}
95