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

FileCacheDriver::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
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