MongoDBCache   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 16
c 4
b 1
f 0
dl 0
loc 97
ccs 19
cts 20
cp 0.95
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A doFetch() 0 3 1
A doDelete() 0 3 1
A doContains() 0 3 1
A doGetStats() 0 3 1
A doSave() 0 3 1
A __construct() 0 9 3
A doFlush() 0 3 1
1
<?php
2
3
namespace Doctrine\Common\Cache;
4
5
use InvalidArgumentException;
6
use MongoCollection;
7
use MongoDB\Collection;
8
use const E_USER_DEPRECATED;
9
use function trigger_error;
10
11
/**
12
 * MongoDB cache provider.
13
 */
14
class MongoDBCache extends CacheProvider
15
{
16
    /**
17
     * The data field will store the serialized PHP value.
18
     */
19
    public const DATA_FIELD = 'd';
20
21
    /**
22
     * The expiration field will store a MongoDate value indicating when the
23
     * cache entry should expire.
24
     *
25
     * With MongoDB 2.2+, entries can be automatically deleted by MongoDB by
26
     * indexing this field with the "expireAfterSeconds" option equal to zero.
27
     * This will direct MongoDB to regularly query for and delete any entries
28
     * whose date is older than the current time. Entries without a date value
29
     * in this field will be ignored.
30
     *
31
     * The cache provider will also check dates on its own, in case expired
32
     * entries are fetched before MongoDB's TTLMonitor pass can expire them.
33
     *
34
     * @see http://docs.mongodb.org/manual/tutorial/expire-data/
35
     */
36
    public const EXPIRATION_FIELD = 'e';
37
38
    /** @var CacheProvider */
39
    private $provider;
40
41
    /**
42
     * This provider will default to the write concern and read preference
43
     * options set on the collection instance (or inherited from MongoDB or
44
     * MongoClient). Using an unacknowledged write concern (< 1) may make the
45
     * return values of delete() and save() unreliable. Reading from secondaries
46
     * may make contain() and fetch() unreliable.
47
     *
48
     * @see http://www.php.net/manual/en/mongo.readpreferences.php
49
     * @see http://www.php.net/manual/en/mongo.writeconcerns.php
50
     *
51
     * @param MongoCollection|Collection $collection
52
     */
53 156
    public function __construct($collection)
54
    {
55 156
        if ($collection instanceof MongoCollection) {
56 78
            @trigger_error('Using a MongoCollection instance for creating a cache adapter is deprecated and will be removed in 2.0', E_USER_DEPRECATED);
57 78
            $this->provider = new LegacyMongoDBCache($collection);
58 78
        } elseif ($collection instanceof Collection) {
0 ignored issues
show
introduced by
$collection is always a sub-type of MongoDB\Collection.
Loading history...
59 78
            $this->provider = new ExtMongoDBCache($collection);
60
        } else {
61
            throw new InvalidArgumentException('Invalid collection given - expected a MongoCollection or MongoDB\Collection instance');
62
        }
63 156
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 152
    protected function doFetch($id)
69
    {
70 152
        return $this->provider->doFetch($id);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 146
    protected function doContains($id)
77
    {
78 146
        return $this->provider->doContains($id);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 148
    protected function doSave($id, $data, $lifeTime = 0)
85
    {
86 148
        return $this->provider->doSave($id, $data, $lifeTime);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 90
    protected function doDelete($id)
93
    {
94 90
        return $this->provider->doDelete($id);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 4
    protected function doFlush()
101
    {
102 4
        return $this->provider->doFlush();
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 2
    protected function doGetStats()
109
    {
110 2
        return $this->provider->doGetStats();
111
    }
112
}
113