Completed
Pull Request — master (#6001)
by Luís
09:11
created

TimestampQueryCacheValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 39
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isValid() 0 12 3
A regionUpdated() 0 10 3
1
<?php
2
3
/*
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the MIT license. For more information, see
18
 * <http://www.doctrine-project.org>.
19
 */
20
21
namespace Doctrine\ORM\Cache;
22
23
/**
24
 * @since   2.5
25
 * @author  Fabio B. Silva <[email protected]>
26
 */
27
class TimestampQueryCacheValidator implements QueryCacheValidator
28
{
29
    /**
30
     * @var TimestampRegion
31
     */
32
    private $timestampRegion;
33
34 82
    public function __construct(TimestampRegion $timestampRegion)
35
    {
36 82
        $this->timestampRegion = $timestampRegion;
37 82
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 34
    public function isValid(QueryCacheKey $key, QueryCacheEntry $entry)
43
    {
44 34
        if ($this->regionUpdated($key, $entry)) {
45 5
            return false;
46
        }
47
48 32
        if ($key->lifetime == 0) {
49 31
            return true;
50
        }
51
52 1
        return ($entry->time + $key->lifetime) > time();
53
    }
54
55 34
    private function regionUpdated(QueryCacheKey $key, QueryCacheEntry $entry)
56
    {
57 34
        if ($key->timestampKey === null) {
58 3
            return false;
59
        }
60
61 31
        $timestamp = $this->timestampRegion->get($key->timestampKey);
62
63 31
        return $timestamp && $timestamp->time > $entry->time;
0 ignored issues
show
Bug introduced by
Accessing time on the interface Doctrine\ORM\Cache\CacheEntry suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
64
    }
65
}
66