Passed
Push — master ( c82647...c877fa )
by Georges
11:01
created

RedisDriverTrait::driverDelete()   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 2
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\Redis;
18
19
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
20
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
21
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
22
use Redis as RedisClient;
23
use RedisCluster as RedisClusterClient;
24
25
/**
26
 * @property RedisClient|RedisClusterClient $instance
27
 * @method Config getConfig()
28
 */
29
trait RedisDriverTrait
30
{
31
    /**
32
     * @param ExtendedCacheItemInterface $item
33
     * @return ?array<string, mixed>
34
     */
35
    protected function driverRead(ExtendedCacheItemInterface $item): ?array
36
    {
37
        $val = $this->instance->get($item->getKey());
38
        if (!$val) {
39
            return null;
40
        }
41
42
        return $this->decode($val);
0 ignored issues
show
Bug introduced by
It seems like decode() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

42
        return $this->/** @scrutinizer ignore-call */ decode($val);
Loading history...
43
    }
44
45
    /**
46
     * @param ExtendedCacheItemInterface ...$items
47
     * @return array<array<string, mixed>>
48
     * @throws \Phpfastcache\Exceptions\PhpfastcacheDriverException
49
     * @throws \RedisException
50
     */
51
    protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array
52
    {
53
        $keys = $this->getKeys($items);
0 ignored issues
show
Bug introduced by
It seems like getKeys() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

53
        /** @scrutinizer ignore-call */ 
54
        $keys = $this->getKeys($items);
Loading history...
54
55
        return array_combine($keys, array_map(
56
            fn($val) => $val ? $this->decode($val) : null,
57
            $this->instance->mGet($keys)
58
        ));
59
    }
60
61
    /**
62
     * @param ExtendedCacheItemInterface $item
63
     * @return mixed
64
     * @throws PhpfastcacheInvalidArgumentException
65
     * @throws PhpfastcacheLogicException
66
     */
67
    protected function driverWrite(ExtendedCacheItemInterface $item): bool
68
    {
69
        $this->assertCacheItemType($item, self::getItemClass());
0 ignored issues
show
Bug introduced by
It seems like assertCacheItemType() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

69
        $this->/** @scrutinizer ignore-call */ 
70
               assertCacheItemType($item, self::getItemClass());
Loading history...
70
71
        $ttl = $item->getExpirationDate()->getTimestamp() - time();
72
73
        /**
74
         * @see https://redis.io/commands/setex
75
         * @see https://redis.io/commands/expire
76
         */
77
        if ($ttl <= 0) {
78
            return $this->instance->expire($item->getKey(), 0);
79
        }
80
81
        return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->driverPreWrap($item)));
0 ignored issues
show
Bug introduced by
It seems like driverPreWrap() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

81
        return $this->instance->setex($item->getKey(), $ttl, $this->encode($this->/** @scrutinizer ignore-call */ driverPreWrap($item)));
Loading history...
Bug introduced by
It seems like encode() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

81
        return $this->instance->setex($item->getKey(), $ttl, $this->/** @scrutinizer ignore-call */ encode($this->driverPreWrap($item)));
Loading history...
82
    }
83
84
    /**
85
     * @param string $key
86
     * @param string $encodedKey
87
     * @return bool
88
     * @throws \RedisException
89
     */
90
    protected function driverDelete(string $key, string $encodedKey): bool
91
    {
92
        return (bool) $this->instance->del($key);
93
    }
94
95
    /**
96
     * @param string[] $keys
97
     * @return bool
98
     */
99
    protected function driverDeleteMultiple(array $keys): bool
100
    {
101
        return (bool) $this->instance->del(...$keys);
102
    }
103
}
104