Watcher::startWatching()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 1
nop 1
1
<?php
2
3
namespace BeyondCode\DuskDashboard;
4
5
use Closure;
6
use React\EventLoop\LoopInterface;
7
use Symfony\Component\Finder\Finder;
8
use Yosymfony\ResourceWatcher\Crc32ContentHash;
9
use Yosymfony\ResourceWatcher\ResourceCacheMemory;
10
use Yosymfony\ResourceWatcher\ResourceWatcher;
11
12
class Watcher
13
{
14
    /** @var \Symfony\Component\Finder\Finder */
15
    protected $finder;
16
17
    /** @var \React\EventLoop\LoopInterface */
18
    protected $loop;
19
20
    public function __construct(Finder $finder, LoopInterface $loop)
21
    {
22
        $this->finder = $finder;
23
        $this->loop = $loop;
24
    }
25
26
    public function startWatching(Closure $callback)
27
    {
28
        $hashContent = new Crc32ContentHash();
29
        $watcher = new ResourceWatcher(new ResourceCacheMemory(), $this->finder, $hashContent);
30
31
        $this->loop->addPeriodicTimer(1 / 2, function () use ($watcher, $callback) {
32
            $watcher_result = $watcher->findChanges();
33
34
            if ($watcher_result->hasChanges()) {
35
                call_user_func($callback);
36
            }
37
        });
38
    }
39
}
40