Completed
Pull Request — master (#112)
by Christophe
02:45
created

MemoryCache::isFresh()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
* This file is part of the Behat Gherkin.
5
* (c) Konstantin Kudryashov <[email protected]>
6
*
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
9
*/
10
11
namespace Behat\Gherkin\Cache;
12
13
use Behat\Gherkin\Node\FeatureNode;
14
15
/**
16
 * Memory cache.
17
 * Caches feature into a memory.
18
 *
19
 * @author Konstantin Kudryashov <[email protected]>
20
 */
21
class MemoryCache implements CacheInterface
22
{
23
    private $features = array();
24
    private $timestamps = array();
25
26
    /**
27
     * Checks that cache for feature exists and is fresh.
28
     *
29
     * @param string  $path      Feature path
30
     * @param integer $timestamp The last time feature was updated
31
     *
32
     * @return Boolean
33
     */
34 3
    public function isFresh($path, $timestamp)
35
    {
36 3
        if (!isset($this->features[$path])) {
37 1
            return false;
38
        }
39
40 2
        return $this->timestamps[$path] > $timestamp;
41
    }
42
43
    /**
44
     * Reads feature cache from path.
45
     *
46
     * @param string $path Feature path
47
     *
48
     * @return FeatureNode
49
     */
50 1
    public function read($path)
51
    {
52 1
        return $this->features[$path];
53
    }
54
55
    /**
56
     * Caches feature node.
57
     *
58
     * @param string      $path    Feature path
59
     * @param FeatureNode $feature Feature instance
60
     */
61 3
    public function write($path, FeatureNode $feature)
62
    {
63 3
        $this->features[$path]   = $feature;
64 3
        $this->timestamps[$path] = time();
65 3
    }
66
}
67