Completed
Pull Request — final (#340)
by
unknown
02:44
created

Driver::driverClear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Lucas Brucksch <[email protected]>
11
 *
12
 */
13
14
namespace phpFastCache\Drivers\Zendshm;
15
16
use phpFastCache\Core\DriverAbstract;
17
use phpFastCache\Core\StandardPsr6StructureTrait;
18
use phpFastCache\Entities\driverStatistic;
19
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
20
use phpFastCache\Exceptions\phpFastCacheDriverException;
21
use Psr\Cache\CacheItemInterface;
22
23
/**
24
 * Class Driver (zend memory cache)
25
 * Requires Zend Data Cache Functions from ZendServer
26
 * @package phpFastCache\Drivers
27
 */
28
class Driver extends DriverAbstract
29
{
30
    /**
31
     * Driver constructor.
32
     * @param array $config
33
     * @throws phpFastCacheDriverException
34
     */
35
    public function __construct(array $config = [])
36
    {
37
        $this->setup($config);
38
39
        if (!$this->driverCheck()) {
40
            throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
41
        }
42
    }
43
44
    /**
45
     * @return bool
46
     */
47
    public function driverCheck()
48
    {
49
        if (extension_loaded('Zend Data Cache') && function_exists('zend_shm_cache_store')) {
50
            return true;
51
        } else {
52
            return false;
53
        }
54
    }
55
56
    /**
57
     * @param \Psr\Cache\CacheItemInterface $item
58
     * @return mixed
59
     * @throws \InvalidArgumentException
60
     */
61
    protected function driverWrite(CacheItemInterface $item)
62
    {
63
        /**
64
         * Check for Cross-Driver type confusion
65
         */
66
        if ($item instanceof Item) {
0 ignored issues
show
Bug introduced by
The class phpFastCache\Drivers\Zendshm\Item does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
67
            $ttl = $item->getExpirationDate()->getTimestamp() - time();
68
69
            return zend_shm_cache_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0));
70
        } else {
71
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
72
        }
73
    }
74
75
    /**
76
     * @param \Psr\Cache\CacheItemInterface $item
77
     * @return mixed
78
     */
79
    protected function driverRead(CacheItemInterface $item)
80
    {
81
        $data = zend_shm_cache_fetch($item->getKey());
82
        if ($data === false) {
83
            return null;
84
        }
85
86
        return $data;
87
    }
88
89
    /**
90
     * @param \Psr\Cache\CacheItemInterface $item
91
     * @return bool
92
     * @throws \InvalidArgumentException
93
     */
94
    protected function driverDelete(CacheItemInterface $item)
95
    {
96
        /**
97
         * Check for Cross-Driver type confusion
98
         */
99
        if ($item instanceof Item) {
0 ignored issues
show
Bug introduced by
The class phpFastCache\Drivers\Zendshm\Item does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
100
            return zend_shm_cache_delete($item->getKey());
101
        } else {
102
            throw new \InvalidArgumentException('Cross-Driver type confusion detected');
103
        }
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    protected function driverClear()
110
    {
111
        return @zend_shm_cache_clear();
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    protected function driverConnect()
118
    {
119
        return true;
120
    }
121
122
    /********************
123
     *
124
     * PSR-6 Extended Methods
125
     *
126
     *******************/
127
128
    /**
129
     * @return driverStatistic
130
     */
131
    public function getStats()
132
    {
133
        $stats = (array) zend_shm_cache_info();
134
        return (new driverStatistic())
135
            ->setData(implode(', ', array_keys($this->namespaces)))
0 ignored issues
show
Bug introduced by
The property namespaces does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
136
            ->setInfo(sprintf("The Zend memory have %d item(s) in cache.\n For more information see RawData.",$stats[ 'items_total' ]))
137
            ->setRawData($stats)
138
            ->setSize($stats[ 'memory_total' ]);
139
    }
140
}