Passed
Push — master ( cbb991...62e47c )
by Albert
07:16 queued 05:08
created

FSEventParser::toEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SwooleTW\Http\HotReload;
4
5
use Carbon\Carbon;
6
7
/**
8
 * Class FSEventParser
9
 */
10
class FSEventParser
11
{
12
    protected const REGEX = '/^([\S+]{3}\s+[\S+]{3}\s+[\d+]{1,2}\s+[\d+]{2}:[\d+]{2}:{0,2}:[\d+]{2}:{0,2}\s+[\d+]{0,4})\s*(\/[\S+]*)\s+([\S+*\s+]*)/mi';
13
14
    protected const DATE = 1;
15
    protected const PATH = 2;
16
    protected const EVENTS = 3;
17
18
    /**
19
     * @param string $event
20
     *
21
     * @return \SwooleTW\Http\HotReload\FSEvent
22
     */
23
    public static function toEvent(string $event): ?FSEvent
24
    {
25
        if (preg_match(static::REGEX, $event, $matches)) {
26
            $date = Carbon::parse($matches[static::DATE]);
27
            $path = $matches[static::PATH];
28
            $events = explode(' ', $matches[static::EVENTS]);
29
            $events = array_intersect(FSEvent::getPossibleTypes(), $events);
30
            asort($events);
31
32
            return new FSEvent($date, $path, $events);
33
        }
34
35
        return null;
36
    }
37
}