DriverPoolAbstractTrait   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A driverWriteMultiple() 0 7 1
A driverReadMultiple() 0 3 1
A driverReadAllKeys() 0 7 1
A assertCacheItemType() 0 5 2
A driverDeleteMultiple() 0 3 1
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\Core\Pool;
18
19
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
20
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
21
use Phpfastcache\Exceptions\PhpfastcacheUnsupportedMethodException;
22
use Phpfastcache\Wiki;
23
use Psr\Cache\CacheItemInterface;
24
25
trait DriverPoolAbstractTrait
26
{
27
    /**
28
     * @return bool
29
     */
30
    abstract protected function driverCheck(): bool;
31
32
    /**
33
     * @return bool
34
     */
35
    abstract protected function driverConnect(): bool;
36
37
    /**
38
     * @param ExtendedCacheItemInterface $item
39
     * @return ?array<string, mixed>
40
     */
41
    abstract protected function driverRead(ExtendedCacheItemInterface $item): ?array;
42
43
    /**
44
     * @param ExtendedCacheItemInterface ...$items
45
     * @return array<array<string, mixed>>
46
     * @throws PhpfastcacheUnsupportedMethodException
47
     */
48
    protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array
49
    {
50
        throw new PhpfastcacheUnsupportedMethodException();
51
    }
52
53
    /**
54
     * @return \Traversable<int, string>
55
     * @throws PhpfastcacheUnsupportedMethodException
56
     */
57
    protected function driverReadAllKeys(string $pattern = ''): iterable
58
    {
59
        throw new PhpfastcacheUnsupportedMethodException(
60
            sprintf(
61
                'The "readAll" operation is unsupported by the the "%s" driver. See the Wiki for more information at %s',
62
                $this->getDriverName(),
0 ignored issues
show
Bug introduced by
It seems like getDriverName() 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

62
                $this->/** @scrutinizer ignore-call */ 
63
                       getDriverName(),
Loading history...
63
                Wiki::FETCH_ALL_KEY_URL,
64
            )
65
        );
66
    }
67
68
    /**
69
     * @param ExtendedCacheItemInterface $item
70
     * @return bool
71
     */
72
    abstract protected function driverWrite(ExtendedCacheItemInterface $item): bool;
73
74
    /**
75
     * @param ExtendedCacheItemInterface ...$item
76
     * @return bool
77
     * @throws PhpfastcacheUnsupportedMethodException
78
     */
79
    protected function driverWriteMultiple(ExtendedCacheItemInterface ...$item): bool
80
    {
81
        /**
82
         * @todo Implement bulk writes to be for v10:
83
         * For methods commit() and saveMultiple()
84
         */
85
        throw new PhpfastcacheUnsupportedMethodException();
86
    }
87
88
89
    /**
90
     * @param string $key
91
     * @param string $encodedKey
92
     * @return bool
93
     */
94
    abstract protected function driverDelete(string $key, string $encodedKey): bool;
95
96
    /**
97
     * @param string[] $keys
98
     * @return bool
99
     * @throws PhpfastcacheUnsupportedMethodException
100
     */
101
    protected function driverDeleteMultiple(array $keys): bool
102
    {
103
        throw new PhpfastcacheUnsupportedMethodException();
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    abstract protected function driverClear(): bool;
110
111
    /**
112
     * @param CacheItemInterface $item
113
     * @param string $expectedClassType
114
     * @throws PhpfastcacheInvalidArgumentException
115
     */
116
    protected function assertCacheItemType(CacheItemInterface $item, string $expectedClassType): void
117
    {
118
        if (!($item instanceof $expectedClassType)) {
119
            throw new PhpfastcacheInvalidArgumentException(
120
                \sprintf('Cross-driver type confusion detected: Expected "%s" object, got "%s"', $expectedClassType, $item::class)
121
            );
122
        }
123
    }
124
}
125