Completed
Pull Request — master (#10)
by Tomáš
04:50 queued 01:55
created

FilesystemDataSource::refresh()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 0
cts 34
cp 0
rs 8.4751
c 0
b 0
f 0
cc 6
eloc 25
nc 6
nop 1
crap 42
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\Source;
13
14
use Dflydev\Canal\Analyzer\Analyzer;
15
use dflydev\util\antPathMatcher\AntPathMatcher;
16
use Symplify\PHP7_Sculpin\Util\PathNormalizer;
17
use Symfony\Component\Finder\Finder;
18
19
final class FilesystemDataSource implements DataSourceInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    private $sourceDir;
25
26
    /**
27
     * @var array
28
     */
29
    private $rawPaths;
30
31
    /**
32
     * @var AntPathMatcher
33
     */
34
    private $matcher;
35
36
    /**
37
     * @var Analyzer
38
     */
39
    private $analyzer;
40
41
    /**
42
     * @var string
43
     */
44
    private $sinceTime;
45
46
    public function __construct(
47
        string $sourceDir,
48
        array $raws,
49
        AntPathMatcher $matcher = null,
50
        Analyzer $analyzer = null
51
    ) {
52
        $this->sourceDir = $sourceDir;
53
        $this->rawPaths = $raws;
54
        $this->matcher = $matcher ?: new AntPathMatcher();
55
        $this->analyzer = $analyzer;
56
        $this->sinceTime = '1970-01-01T00:00:00Z';
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function dataSourceId()
63
    {
64
        return 'FilesystemDataSource:'.$this->sourceDir;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function refresh(SourceSet $sourceSet)
71
    {
72
        $sinceTimeLast = $this->sinceTime;
73
74
        $this->sinceTime = date('c');
75
76
        $files = (new Finder())
77
            ->files()
78
            ->ignoreVCS(true)
79
            ->ignoreDotFiles(false)
80
            ->date('>='.$sinceTimeLast)
81
            ->followLinks()
82
            ->in($this->sourceDir);
83
84
        $sinceTimeLastSeconds = strtotime($sinceTimeLast);
85
86
        foreach ($files as $file) {
87
            if ($sinceTimeLastSeconds > $file->getMTime()) {
88
                // This is a hack because Finder is actually incapable
89
                // of resolution down to seconds.
90
91
                // Sometimes this may result in the file looking like it
92
                // has been modified twice in a row when it has not.
93
                continue;
94
            }
95
96
            $isRaw = false;
97
98
            foreach ($this->rawPaths as $pattern) {
99
                if (!$this->matcher->isPattern($pattern)) {
100
                    continue;
101
                }
102
                if ($this->matcher->match(
103
                        $pattern,
104
                        PathNormalizer::normalize($file->getRelativePathname())
105
                    )
106
                ) {
107
                    $isRaw = true;
108
                    break;
109
                }
110
            }
111
112
            $source = new FileSource($this->analyzer, $this, $file, $isRaw, true);
113
            $sourceSet->mergeSource($source);
114
        }
115
    }
116
}
117