Passed
Pull Request — master (#262)
by Pascal
02:32
created

StdListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 38
rs 10
c 2
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A forwardedEvents() 0 3 1
A handle() 0 8 2
A get() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\FFMpeg;
4
5
use Alchemy\BinaryDriver\Listeners\ListenerInterface;
6
use Evenement\EventEmitter;
7
use Symfony\Component\Process\Process;
8
9
class StdListener extends EventEmitter implements ListenerInterface
10
{
11
    /**
12
     * Name of the emitted event.
13
     *
14
     * @var string
15
     */
16
    private $eventName;
17
18
    public function __construct(string $eventName = 'listen')
19
    {
20
        $this->eventName = $eventName;
21
    }
22
23
    private $data = [
24
        Process::ERR => [],
25
        Process::OUT => [],
26
    ];
27
28
    public function handle($type, $data)
29
    {
30
        $lines = preg_split('/\n|\r\n?/', $data);
31
32
        foreach ($lines as $line) {
33
            $line = $this->data[$type][] = trim($line);
34
35
            $this->emit($this->eventName, [$line, $type]);
36
        }
37
    }
38
39
    public function get(): array
40
    {
41
        return $this->data;
42
    }
43
44
    public function forwardedEvents()
45
    {
46
        return [$this->eventName];
47
    }
48
}
49