Completed
Push — develop ( 960a7a...f8a065 )
by Baptiste
06:45
created

FormatsFactory::make()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Rest\ServerBundle\Factory;
5
6
use Innmind\Rest\Server\{
7
    Formats,
8
    Format\Format,
9
    Format\MediaType
10
};
11
use Innmind\Immutable\{
12
    Map,
13
    Set
14
};
15
16
final class FormatsFactory
17
{
18 8
    public function make(array $formats): Formats
19
    {
20 8
        $map = new Map('string', Format::class);
21
22 8
        foreach ($formats as $name => $format) {
23 8
            $map = $map->put(
24
                $name,
25 8
                $this->makeFormat($name, $format)
26
            );
27
        }
28
29 8
        return new Formats($map);
30
    }
31
32 8
    private function makeFormat(string $name, array $config): Format
33
    {
34 8
        $mediaTypes = new Set(MediaType::class);
35
36 8
        foreach ($config['media_types'] as $mediaType => $priority) {
37 8
            $mediaTypes = $mediaTypes->add(
38 8
                new MediaType($mediaType, $priority)
39
            );
40
        }
41
42 8
        return new Format($name, $mediaTypes, $config['priority']);
43
    }
44
}
45