Completed
Push — master ( 28797f...652832 )
by Alexander
01:44
created

FileDependency   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 7
dl 0
loc 28
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A generateDependencyData() 0 8 2
1
<?php
2
namespace Yiisoft\Cache\Dependency;
3
4
use Yiisoft\Cache\CacheInterface;
5
use Yiisoft\Cache\Exception\InvalidConfigException;
6
7
/**
8
 * FileDependency represents a dependency based on a file's last modification time.
9
 *
10
 * If the last modification time of the file specified via {@see fileName} is changed,
11
 * the dependency is considered as changed.
12
 *
13
 * For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview).
14
 */
15
final class FileDependency extends Dependency
16
{
17
    private $fileName;
18
19
    /**
20
     * @param string $fileName the file path or [path alias](guide:concept-aliases) whose last modification time is used to
21
     * check if the dependency has been changed.
22
     */
23
    public function __construct(string $fileName)
24
    {
25
        $this->fileName = $fileName;
26
    }
27
28
    /**
29
     * Generates the data needed to determine if dependency has been changed.
30
     * This method returns the file's last modification time.
31
     * @param CacheInterface $cache the cache component that is currently evaluating this dependency
32
     * @return mixed the data needed to determine if dependency has been changed.
33
     * @throws InvalidConfigException if {@see fileName} is not set
34
     */
35
    protected function generateDependencyData(CacheInterface $cache)
36
    {
37
        if ($this->fileName === null) {
38
            throw new InvalidConfigException('FileDependency::fileName must be set');
39
        }
40
41
        clearstatcache(false, $this->fileName);
42
        return @filemtime($this->fileName);
43
    }
44
}
45