Passed
Push — master ( 4006b8...c871f9 )
by Georges
02:32 queued 21s
created

Driver::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * This file is part of Phpfastcache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phpfastcache\Drivers\Leveldb;
18
19
use LevelDB as LeveldbClient;
20
use Phpfastcache\Cluster\AggregatablePoolInterface;
21
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
22
use Phpfastcache\Core\Pool\IO\IOHelperTrait;
23
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
24
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
25
use Phpfastcache\Exceptions\PhpfastcacheCoreException;
26
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
27
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
28
29
/**
30
 * @property LeveldbClient|null $instance Instance of driver service
31
 * @method Config getConfig()
32
 */
33
class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface
34
{
35
    use IOHelperTrait;
36
37
    protected const LEVELDB_FILENAME = '.database';
38
39
    /**
40
     * @return bool
41
     */
42
    public function driverCheck(): bool
43
    {
44
        return extension_loaded('Leveldb');
45
    }
46
47
    /**
48
     * Close connection on destruct
49
     */
50
    public function __destruct()
51
    {
52
        if ($this->instance instanceof LeveldbClient) {
53
            $this->instance->close();
54
            $this->instance = null;
55
        }
56
    }
57
58
    /**
59
     * @param ExtendedCacheItemInterface $item
60
     * @return null|array
61
     */
62
    protected function driverRead(ExtendedCacheItemInterface $item): ?array
63
    {
64
        $val = $this->instance->get($item->getKey());
0 ignored issues
show
Bug introduced by
The method get() 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

64
        /** @scrutinizer ignore-call */ 
65
        $val = $this->instance->get($item->getKey());

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...
65
        if (!$val) {
66
            return null;
67
        }
68
69
        return $this->decode($val);
70
    }
71
72
    /**
73
     * @param ExtendedCacheItemInterface $item
74
     * @return bool
75
     * @throws PhpfastcacheInvalidArgumentException
76
     * @throws PhpfastcacheLogicException
77
     */
78
    protected function driverWrite(ExtendedCacheItemInterface $item): bool
79
    {
80
        $this->assertCacheItemType($item, Item::class);
81
82
        return (bool)$this->instance->set($item->getKey(), $this->encode($this->driverPreWrap($item)));
83
    }
84
85
    /**
86
     * @param ExtendedCacheItemInterface $item
87
     * @return bool
88
     * @throws PhpfastcacheInvalidArgumentException
89
     */
90
    protected function driverDelete(ExtendedCacheItemInterface $item): bool
91
    {
92
        $this->assertCacheItemType($item, Item::class);
93
94
        return $this->instance->delete($item->getKey());
95
    }
96
97
    /**
98
     * @return bool
99
     * @throws PhpfastcacheCoreException
100
     * @throws PhpfastcacheLogicException
101
     */
102
    protected function driverClear(): bool
103
    {
104
        if ($this->instance instanceof LeveldbClient) {
105
            $this->instance->close();
106
            $this->instance = null;
107
        }
108
        $result = LeveldbClient::destroy($this->getLeveldbFile());
109
        $this->driverConnect();
110
111
        return $result;
112
    }
113
114
    /**
115
     * @return string
116
     * @throws PhpfastcacheCoreException
117
     */
118
    public function getLeveldbFile(): string
119
    {
120
        return $this->getPath() . '/' . self::LEVELDB_FILENAME;
121
    }
122
123
    /**
124
     * @return bool
125
     * @throws PhpfastcacheCoreException
126
     * @throws PhpfastcacheLogicException
127
     */
128
    protected function driverConnect(): bool
129
    {
130
        if ($this->instance instanceof LeveldbClient) {
131
            throw new PhpfastcacheLogicException('Already connected to Leveldb database');
132
        }
133
134
        $this->instance = new LeveldbClient($this->getLeveldbFile());
135
136
        return true;
137
    }
138
}
139