Completed
Pull Request — develop (#27)
by Chris
01:41
created

Lines   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 48
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A read() 0 11 2
A write() 0 4 1
A goNext() 0 13 3
A isValid() 0 4 1
1
<?php
2
3
namespace Chrisyue\PhpM3u8\M3u8\Lines;
4
5
use Chrisyue\PhpM3u8\M3u8\Transformer\TransformerInterface;
6
use Chrisyue\PhpM3u8\Stream\StreamInterface;
7
8
class Lines implements LinesInterface
9
{
10
    private $stream;
11
12
    private $transformer;
13
14
    public function __construct(StreamInterface $stream, TransformerInterface $transformer)
15
    {
16
        $this->stream = $stream;
17
        $this->transformer = $transformer;
18
    }
19
20
    public function read()
21
    {
22
        static $line;
23
        static $lineInfo;
24
        if ($this->stream->getLine() !== $line) {
25
            $line = $this->stream->getLine();
26
            $lineInfo = $this->transformer->transform($line);
27
        }
28
29
        return $lineInfo;
30
    }
31
32
    public function write(array $lineInfo)
33
    {
34
        $this->stream->putLine($this->transformer->reverse($lineInfo));
35
    }
36
37
    public function goNext()
38
    {
39
        $this->stream->goNext();
40
41
        if (!$this->stream->isValid()) {
42
            return;
43
        }
44
45
        $line = trim($this->stream->getLine());
46
        if (empty($line)) {
47
            $this->goNext();
48
        }
49
    }
50
51
    public function isValid()
52
    {
53
        return $this->stream->isValid();
54
    }
55
}
56