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

ConfigFilesystemDataSource::refresh()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 47
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 0
cts 29
cp 0
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 22
nc 10
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\DotAccessConfiguration\ConfigurationInterface;
15
use dflydev\util\antPathMatcher\AntPathMatcher;
16
use Symplify\PHP7_Sculpin\SiteConfiguration\SiteConfigurationFactory;
17
use Symfony\Component\Finder\Finder;
18
19
final class ConfigFilesystemDataSource implements DataSourceInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    private $sourceDir;
25
26
    /**
27
     * @var ConfigurationInterface
28
     */
29
    private $siteConfiguration;
30
31
    /**
32
     * @var string
33
     */
34
    private $siteConfigurationFactory;
35
36
    /**
37
     * @var AntPathMatcher
38
     */
39
    private $matcher;
40
41
    /**
42
     * @var string
43
     */
44
    private $sinceTime;
45
46
    public function __construct(
47
        string $sourceDir,
48
        ConfigurationInterface $siteConfiguration,
49
        SiteConfigurationFactory $siteConfigurationFactory,
50
        AntPathMatcher $matcher = null
51
    ) {
52
        $this->sourceDir = $sourceDir;
53
        $this->siteConfiguration = $siteConfiguration;
54
        $this->siteConfigurationFactory = $siteConfigurationFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $siteConfigurationFactory of type object<Symplify\PHP7_Scu...teConfigurationFactory> is incompatible with the declared type string of property $siteConfigurationFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
        $this->matcher = $matcher ?: new AntPathMatcher();
56
        $this->sinceTime = '1970-01-01T00:00:00Z';
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function dataSourceId()
63
    {
64
        // This is not really needed since we are not going to
65
        // ever create actual sources.
66
        return 'ConfigFilesystemDataSource:'.$this->sourceDir;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function refresh(SourceSet $sourceSet)
73
    {
74
        if (!is_dir($this->sourceDir)) {
75
            return;
76
        }
77
78
        $sinceTimeLast = $this->sinceTime;
79
80
        $this->sinceTime = date('c');
81
82
        // We regenerate the whole site if any config file changes.
83
        $configFilesHaveChanged = false;
84
85
        $files = (new Finder())
86
            ->files()
87
            ->name('sculpin_site*.yml')
88
            ->date('>='.$sinceTimeLast)
89
            ->in($this->sourceDir);
90
91
        $sinceTimeLastSeconds = strtotime($sinceTimeLast);
92
93
        foreach ($files as $file) {
94
            if ($sinceTimeLastSeconds > $file->getMTime()) {
95
                // This is a hack because Finder is actually incapable
96
                // of resolution down to seconds.
97
98
                // Sometimes this may result in the file looking like it
99
                // has been modified twice in a row when it has not.
100
                continue;
101
            }
102
103
            $configFilesHaveChanged = true;
104
105
            break;
106
        }
107
108
        if ($configFilesHaveChanged) {
109
            $newConfig = $this->siteConfigurationFactory->create();
0 ignored issues
show
Bug introduced by
The method create cannot be called on $this->siteConfigurationFactory (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
110
            $this->siteConfiguration->import($newConfig);
111
112
            // If any of the config files have changed we should
113
            // mark all of the sources as having changed.
114
            foreach ($sourceSet->allSources() as $source) {
115
                $source->setHasChanged();
116
            }
117
        }
118
    }
119
}
120