MediaInfoContainerBuilder::addAttributes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Mhor\MediaInfo\Builder;
4
5
use Mhor\MediaInfo\Container\MediaInfoContainer;
6
use Mhor\MediaInfo\Factory\AttributeFactory;
7
use Mhor\MediaInfo\Factory\TypeFactory;
8
use Mhor\MediaInfo\Type\AbstractType;
9
10
class MediaInfoContainerBuilder
11
{
12
    /**
13
     * @var MediaInfoContainer
14
     */
15
    private $mediaInfoContainer;
16
17
    /**
18
     * @var TypeFactory
19
     */
20
    private $typeFactory;
21
22 11
    public function __construct()
23
    {
24 11
        $this->mediaInfoContainer = new MediaInfoContainer();
25 11
        $this->typeFactory = new TypeFactory();
26 11
    }
27
28
    /**
29
     * @return MediaInfoContainer
30
     */
31 8
    public function build(): \Mhor\MediaInfo\Container\MediaInfoContainer
32
    {
33 8
        return $this->mediaInfoContainer;
34
    }
35
36
    /**
37
     * @param string $version
38
     */
39 5
    public function setVersion($version): void
40
    {
41 5
        $this->mediaInfoContainer->setVersion($version);
42 5
    }
43
44
    /**
45
     * @param $typeName
46
     * @param array $attributes
47
     *
48
     * @throws \Mhor\MediaInfo\Exception\UnknownTrackTypeException
49
     */
50 8
    public function addTrackType($typeName, array $attributes): void
51
    {
52 8
        $trackType = $this->typeFactory->create($typeName);
53 7
        $this->addAttributes($trackType, $attributes);
54
55 7
        $this->mediaInfoContainer->add($trackType);
56 7
    }
57
58
    /**
59
     * @param AbstractType $trackType
60
     * @param array        $attributes
61
     */
62 7
    private function addAttributes(AbstractType $trackType, array $attributes): void
63
    {
64 7
        foreach ($this->sanitizeAttributes($attributes) as $attribute => $value) {
65 6
            if ($attribute[0] === '@') {
66 3
                continue;
67
            }
68
69 6
            $trackType->set(
70 6
                $attribute,
71 6
                AttributeFactory::create($attribute, $value)
72
            );
73
        }
74 7
    }
75
76
    /**
77
     * @param array $attributes
78
     *
79
     * @return array
80
     */
81 7
    private function sanitizeAttributes(array $attributes): array
82
    {
83 7
        $sanitizeAttributes = [];
84 7
        foreach ($attributes as $key => $value) {
85 6
            $key = $this->formatAttribute($key);
86 6
            if (isset($sanitizeAttributes[$key])) {
87 4
                if (!is_array($sanitizeAttributes[$key])) {
88 2
                    $sanitizeAttributes[$key] = [$sanitizeAttributes[$key]];
89
                }
90
91 4
                if (!is_array($value)) {
92 4
                    $value = [$value];
93
                }
94
95 4
                $value = $sanitizeAttributes[$key] + $value;
96
            }
97
98 6
            $sanitizeAttributes[$key] = $value;
99
        }
100
101 7
        return $sanitizeAttributes;
102
    }
103
104
    /**
105
     * @param string $attribute
106
     *
107
     * @return string
108
     */
109 6
    private function formatAttribute(string $attribute): string
110
    {
111 6
        return trim(str_replace('__', '_', str_replace(' ', '_', strtolower($attribute))), '_');
112
    }
113
}
114