Completed
Pull Request — develop (#27)
by Chris
05:29
created

DataBuilder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addUri() 0 9 2
B addTag() 0 24 6
A getResult() 0 4 1
1
<?php
2
3
namespace Chrisyue\PhpM3u8\Parser;
4
5
use Chrisyue\PhpM3u8\Definition\TagDefinition;
6
7
class DataBuilder
8
{
9
    private $currentUriAware;
10
11
    private $result;
12
13
    public function __construct()
14
    {
15
        $this->result = new \ArrayObject();
16
    }
17
18
    public function addUri($uri)
19
    {
20
        if (null === $this->currentUriAware) {
21
            throw new DataBuildingException('uri found, but doesn\'t know how to handle it');
22
        }
23
24
        $this->currentUriAware['uri'] = $uri;
25
        $this->currentUriAware = null;
26
    }
27
28
    public function addTag(TagDefinition $definition, $data)
29
    {
30
        $parent = $this->result;
31
        if (null === $this->currentUriAware) {
32
            if ('media-segment' === $definition->getCategory()) {
33
                $this->currentUriAware = new \ArrayObject();
34
                $this->result['mediaSegments'][] = $this->currentUriAware;
35
            } elseif ($definition->isUriAware()) {
36
                $this->currentUriAware = $data;
37
            }
38
        }
39
40
        if ('media-segment' === $definition->getCategory()) {
41
            $parent = $this->currentUriAware;
42
        }
43
44
        if ($definition->isMultiple()) {
45
            $parent[$definition->getProperty()][] = $data;
46
47
            return;
48
        }
49
50
        $parent[$definition->getProperty()] = $data;
51
    }
52
53
    public function getResult()
54
    {
55
        return $this->result;
56
    }
57
}
58