Memory::exists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace kalanis\kw_cache\Simple;
4
5
6
use kalanis\kw_cache\Interfaces\ICache;
7
8
9
/**
10
 * Class Memory
11
 * @package kalanis\kw_cache\Simple
12
 * Caching content in memory
13
 */
14
class Memory implements ICache
15
{
16
    /** @var resource|null */
17
    protected $resource = null;
18
19 2
    public function init(array $what): void
20
    {
21 2
    }
22
23 2
    public function exists(): bool
24
    {
25 2
        return !is_null($this->resource);
26
    }
27
28 2
    public function set(string $content): bool
29
    {
30 2
        $this->clear();
31 2
        $resource = fopen('php://temp', 'rb+');
32 2
        if (false === $resource) {
33
            // @codeCoverageIgnoreStart
34
            return false;
35
        }
36
        // @codeCoverageIgnoreEnd
37 2
        fwrite($resource, $content);
38 2
        $this->resource = $resource;
39 2
        return true;
40
    }
41
42 2
    public function get(): string
43
    {
44 2
        if ($this->exists()) {
45 2
            rewind($this->resource);
46 2
            return strval(stream_get_contents($this->resource, -1, 0));
47
        } else {
48 2
            return '';
49
        }
50
    }
51
52 2
    public function clear(): void
53
    {
54 2
        if ($this->exists()) {
55 2
            fclose($this->resource);
56
        }
57 2
        $this->resource = null;
58 2
    }
59
}
60