FileIndexer::add()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4
1
<?php
2
3
namespace JK\SamBundle\Watcher\Indexer;
4
5
use Exception;
6
use SplFileInfo;
7
use Symfony\Component\Filesystem\Filesystem;
8
use Symfony\Component\Finder\Finder;
9
10
class FileIndexer implements FileIndexerInterface
11
{
12
    /**
13
     * @var integer[]
14
     */
15
    protected $index = [];
16
17
    /**
18
     * @var SplFileInfo[]
19
     */
20
    protected $changes = [];
21
22
    /**
23
     * Index a given directory : add each found files (according to the given extension) to the index.
24
     *
25
     * @param array $directories
26
     * @param array $extensions
27
     *
28
     * @throws Exception
29
     */
30 4
    public function index(array $directories, array $extensions = [])
31
    {
32 4
        $fileSystem = new Filesystem();
33 4
        $finder = new Finder();
34
35
        // reset the change set
36 4
        $this->changes = [];
37 4
        clearstatcache();
38
39 4
        foreach ($directories as $directory) {
40 4
            if (!$fileSystem->exists($directory)) {
41 1
                throw new Exception('The directory '.$directory.' does not exists');
42
            }
43
44
            // find all files in the given directory
45
            $finder
46 3
                ->files()
47 3
                ->in($directory)
48
            ;
49
50
            // eventually filtering by file extension
51 3
            foreach ($extensions as $extension) {
52 2
                $finder->name('*.'.$extension);
53
            }
54
55
            // index the results
56 3
            foreach ($finder as $file) {
57 3
                $this->add($file);
58
            }
59
        }
60
61 3
    }
62
63
    /**
64
     * Add an entry to the indexer. If a entry already exists, it will also be added to the changes.
65
     *
66
     * @param SplFileInfo $splFileInfo
67
     *
68
     * @throws Exception
69
     */
70 4
    public function add(SplFileInfo $splFileInfo)
71
    {
72 4
        $fileSystem = new Filesystem();
73
74 4
        if (!$fileSystem->exists($splFileInfo->getRealPath())) {
75 1
            throw new Exception('Trying to add '.$splFileInfo->getRealPath().' missing file to the index');
76
        }
77
78
        // if the file is already present in the index, and its mtime has been modified, we add it to the change set
79 3
        if ($this->has($splFileInfo->getRealPath())
80 3
            && $splFileInfo->getMTime() !== $this->index[$splFileInfo->getRealPath()]) {
81 1
            $this->changes[$splFileInfo->getRealPath()] = $splFileInfo;
82
        }
83
        // add new or existing file to the index
84 3
        $this->index[$splFileInfo->getRealPath()] = $splFileInfo->getMTime();
85 3
    }
86
87
    /**
88
     * Return an existing index entry.
89
     *
90
     * @param string $entryName
91
     *
92
     * @return SplFileInfo
93
     *
94
     * @throws Exception
95
     */
96 4
    public function get($entryName)
97
    {
98 4
        if (!$this->has($entryName)) {
99 1
            throw new Exception('Trying to get invalid index entry : '.$entryName);
100
        }
101
102 3
        return new SplFileInfo($entryName);
103
    }
104
105
    /**
106
     * Return true if the entry exists, false otherwise.
107
     *
108
     * @param $entryName
109
     *
110
     * @return bool
111
     */
112 4
    public function has($entryName)
113
    {
114 4
        return array_key_exists($entryName, $this->index);
115
    }
116
117
    /**
118
     * Return true if the indexer has new changes since last index.
119
     *
120
     * @return bool
121
     */
122 1
    public function hasChangedEntries()
123
    {
124 1
        return count($this->changes) > 0;
125
    }
126
127
    /**
128
     * Return the changed entry since last index.
129
     *
130
     * @return SplFileInfo[]
131
     */
132 1
    public function getChangedEntries()
133
    {
134 1
        return $this->changes;
135
    }
136
}
137