Passed
Push — v9 ( 467cbb...1836cb )
by Georges
03:07 queued 24s
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\Ssdb;
18
19
use Phpfastcache\Cluster\AggregatablePoolInterface;
20
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
21
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
22
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
23
use Phpfastcache\Entities\DriverStatistic;
24
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
25
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
26
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
27
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
28
use phpssdb\Core\SimpleSSDB;
0 ignored issues
show
Bug introduced by
The type phpssdb\Core\SimpleSSDB was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use phpssdb\Core\SSDBException;
0 ignored issues
show
Bug introduced by
The type phpssdb\Core\SSDBException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use phpssdb\Core\SSDB;
0 ignored issues
show
Bug introduced by
The type phpssdb\Core\SSDB was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
32
/**
33
 * @property SimpleSSDB $instance Instance of driver service
34
 * @method Config getConfig()
35
 */
36
class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface
37
{
38
    use TaggableCacheItemPoolTrait;
39
40
    /**
41
     * @return bool
42
     */
43
    public function driverCheck(): bool
44
    {
45
        static $driverCheck;
46
47
        return $driverCheck ?? ($driverCheck = class_exists(SSDB::class));
48
    }
49
50
    /**
51
     * @return DriverStatistic
52
     */
53
    public function getStats(): DriverStatistic
54
    {
55
        $stat = new DriverStatistic();
56
        $info = $this->instance->info();
57
58
        /**
59
         * Data returned by Ssdb are very poorly formatted
60
         * using hardcoded offset of pair key-value :-(
61
         */
62
        $stat->setInfo(sprintf("Ssdb-server v%s with a total of %s call(s).\n For more information see RawData.", $info[2], $info[6]))
63
            ->setRawData($info)
64
            ->setData(implode(', ', array_keys($this->itemInstances)))
65
            ->setSize($this->instance->dbsize());
66
67
        return $stat;
68
    }
69
70
    /**
71
     * @return bool
72
     * @throws PhpfastcacheDriverException
73
     */
74
    protected function driverConnect(): bool
75
    {
76
        try {
77
            $clientConfig = $this->getConfig();
78
79
            $this->instance = new SimpleSSDB($clientConfig->getHost(), $clientConfig->getPort(), $clientConfig->getTimeout());
80
            if (!empty($clientConfig->getPassword())) {
81
                $this->instance->auth($clientConfig->getPassword());
82
            }
83
84
            return true;
85
        } catch (SSDBException $e) {
86
            throw new PhpfastcacheDriverCheckException('Ssdb failed to connect with error: ' . $e->getMessage(), 0, $e);
87
        }
88
    }
89
90
    /**
91
     * @param ExtendedCacheItemInterface $item
92
     * @return null|array
93
     */
94
    protected function driverRead(ExtendedCacheItemInterface $item): ?array
95
    {
96
        $val = $this->instance->get($item->getEncodedKey());
97
        if (!$val) {
98
            return null;
99
        }
100
101
        return $this->decode($val);
102
    }
103
104
    /**
105
     * @param ExtendedCacheItemInterface $item
106
     * @return mixed
107
     * @throws PhpfastcacheInvalidArgumentException
108
     * @throws PhpfastcacheLogicException
109
     */
110
    protected function driverWrite(ExtendedCacheItemInterface $item): bool
111
    {
112
        $this->assertCacheItemType($item, Item::class);
113
114
        return (bool)$this->instance->setx($item->getEncodedKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl());
115
    }
116
117
    /**
118
     * @param ExtendedCacheItemInterface $item
119
     * @return bool
120
     * @throws PhpfastcacheInvalidArgumentException
121
     */
122
    protected function driverDelete(ExtendedCacheItemInterface $item): bool
123
    {
124
        $this->assertCacheItemType($item, Item::class);
125
126
        return (bool)$this->instance->del($item->getEncodedKey());
127
    }
128
129
    /**
130
     * @return bool
131
     */
132
    protected function driverClear(): bool
133
    {
134
        $this->instance->flushdb('kv');
135
136
        // Status not returned, then we assume its true
137
        return true;
138
    }
139
}
140