Completed
Push — master ( fbe46c...fa1c37 )
by Max
02:21
created

FileCacheDriver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 53
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 3 1
A add() 0 8 1
A get() 0 7 1
A __construct() 0 3 1
1
<?php
2
3
namespace MidasSoft\DominicanBankParser\Cache;
4
5
class FileCacheDriver extends AbstractCacheDriver
6
{
7
    /**
8
     * The config of the driver.
9
     *
10
     * @var array
11
     */
12
    protected $config = [
13
        'path' => '',
14
        'timezone' => 'America/Santo_Domingo',
15
    ];
16
17
    /**
18
     * Creates a new FileCacheDriver instance.
19
     *
20
     * @param array $config
21
     */
22 4
    public function __construct(array $config)
23
    {
24 4
        $this->config = $config;
25 4
    }
26
27
    /**
28
     * {@inheritDoc}
29
     */
30 4
    public function add($key, $value)
31
    {
32 4
        $filename = sprintf('%s/%s.txt', $this->config['path'], $key);
33 4
        $file = fopen($filename, 'w');
34
35 4
        fwrite($file, serialize($value));
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        fwrite(/** @scrutinizer ignore-type */ $file, serialize($value));
Loading history...
36 4
        fclose($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
        fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
37 4
        $this->keys[] = $key;
38 4
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 1
    public function get($key)
44
    {
45 1
        $filename = sprintf('%s/%s.txt', $this->config['path'], $key);
46 1
        $file = fopen($filename, 'r');
47 1
        $content = fread($file, filesize($filename));
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        $content = fread(/** @scrutinizer ignore-type */ $file, filesize($filename));
Loading history...
48
49 1
        return unserialize($content);
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55 1
    public function remove($key)
56
    {
57 1
        unlink(sprintf('%s/%s.txt', $this->config['path'], $key));
58 1
    }
59
}
60