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

FSEventParser   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A toEvent() 0 13 2
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
}