FileCache::getIds()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php declare(strict_types=1);
2
3
namespace Compolomus\RssReader;
4
5
use SplFileObject;
6
7
class FileCache implements CacheInterface
8
{
9
    public function __construct(
10
        public string          $cacheDir = '/tmp/.rssreader_cache',
11
        public string          $cacheIdsFile = 'cacheIds.txt',
12
        private ?SplFileObject $cacheIds = null,
13
    ) {
14
        // Read path cache dir from env
15
        if (!empty($_ENV['RSSREADER_CACHE_DIR'])) {
16
            $this->cacheDir = $_ENV['RSSREADER_CACHE_DIR'];
17
        }
18
19
        if (!is_dir($this->cacheDir)) {
20
            if (!mkdir($this->cacheDir) && !is_dir($this->cacheDir)) {
21
                throw new \RuntimeException(sprintf('Directory "%s" was not created', $this->cacheDir));
22
            }
23
        }
24
25
        $this->cacheIds = new SplFileObject($this->cacheDir . '/' . $this->cacheIdsFile, 'a+b');
26
    }
27
28
    /**
29
     * @param array $ids List of IDs to save
30
     *
31
     * @return int Amount of saved bytes
32
     */
33
    public function saveIds(array $ids = []): int
34
    {
35
        return $this->cacheIds->fwrite(implode(PHP_EOL, $ids) . PHP_EOL);
0 ignored issues
show
Bug introduced by
The method fwrite() does not exist on null. ( Ignorable by Annotation )

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

35
        return $this->cacheIds->/** @scrutinizer ignore-call */ fwrite(implode(PHP_EOL, $ids) . PHP_EOL);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
    }
37
38
    /**
39
     * Get list of cached IDs
40
     *
41
     * @return array
42
     */
43
    public function getIds(): array
44
    {
45
        $cacheFile = $this->cacheDir . '/' . $this->cacheIdsFile;
46
47
        return
48
            file_exists($cacheFile)
49
                ? file($cacheFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
50
                : [];
51
    }
52
}
53