Passed
Push — master ( 5c1ad9...58f408 )
by Marco
05:40
created

MongoDBCache::doFlush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\Common\Cache;
21
22
use MongoCollection;
23
use MongoDB\Collection;
24
25
/**
26
 * MongoDB cache provider.
27
 *
28
 * @since  1.1
29
 * @author Jeremy Mikola <[email protected]>
30
 */
31
class MongoDBCache extends CacheProvider
32
{
33
    /**
34
     * The data field will store the serialized PHP value.
35
     */
36
    const DATA_FIELD = 'd';
37
38
    /**
39
     * The expiration field will store a MongoDate value indicating when the
40
     * cache entry should expire.
41
     *
42
     * With MongoDB 2.2+, entries can be automatically deleted by MongoDB by
43
     * indexing this field with the "expireAfterSeconds" option equal to zero.
44
     * This will direct MongoDB to regularly query for and delete any entries
45
     * whose date is older than the current time. Entries without a date value
46
     * in this field will be ignored.
47
     *
48
     * The cache provider will also check dates on its own, in case expired
49
     * entries are fetched before MongoDB's TTLMonitor pass can expire them.
50
     *
51
     * @see http://docs.mongodb.org/manual/tutorial/expire-data/
52
     */
53
    const EXPIRATION_FIELD = 'e';
54
55
    /**
56
     * @var CacheProvider
57
     */
58
    private $provider;
59
60
    /**
61
     * Constructor.
62
     *
63
     * This provider will default to the write concern and read preference
64
     * options set on the collection instance (or inherited from MongoDB or
65
     * MongoClient). Using an unacknowledged write concern (< 1) may make the
66
     * return values of delete() and save() unreliable. Reading from secondaries
67
     * may make contain() and fetch() unreliable.
68
     *
69
     * @see http://www.php.net/manual/en/mongo.readpreferences.php
70
     * @see http://www.php.net/manual/en/mongo.writeconcerns.php
71
     * @param MongoCollection|Collection $collection
72
     */
73 154
    public function __construct($collection)
74
    {
75 154
        if ($collection instanceof MongoCollection) {
76 77
            @trigger_error('Using a MongoCollection instance for creating a cache adapter is deprecated and will be removed in 2.0', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
77 77
            $this->provider = new LegacyMongoDBCache($collection);
78 77
        } elseif ($collection instanceof Collection) {
79 77
            $this->provider = new ExtMongoDBCache($collection);
80
        } else {
81
            throw new \InvalidArgumentException('Invalid collection given - expected a MongoCollection or MongoDB\Collection instance');
82
        }
83 154
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 150
    protected function doFetch($id)
89
    {
90 150
        return $this->provider->doFetch($id);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 144
    protected function doContains($id)
97
    {
98 144
        return $this->provider->doContains($id);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 146
    protected function doSave($id, $data, $lifeTime = 0)
105
    {
106 146
        return $this->provider->doSave($id, $data, $lifeTime);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 90
    protected function doDelete($id)
113
    {
114 90
        return $this->provider->doDelete($id);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 4
    protected function doFlush()
121
    {
122 4
        return $this->provider->doFlush();
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 2
    protected function doGetStats()
129
    {
130 2
        return $this->provider->doGetStats();
131
    }
132
}
133