FormatsFactory::make()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
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\ClientBundle;
5
6
use Innmind\Rest\Client\{
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 2
    public function make(array $formats): Formats
19
    {
20 2
        $map = new Map('string', Format::class);
21
22 2
        foreach ($formats as $name => $format) {
23 2
            $map = $map->put(
24 2
                $name,
25 2
                $this->makeFormat($name, $format)
26
            );
27
        }
28
29 2
        return new Formats($map);
30
    }
31
32 2
    private function makeFormat(string $name, array $config): Format
33
    {
34 2
        $mediaTypes = new Set(MediaType::class);
35
36 2
        foreach ($config['media_types'] as $mediaType => $priority) {
37 2
            $mediaTypes = $mediaTypes->add(
38 2
                new MediaType($mediaType, $priority)
39
            );
40
        }
41
42 2
        return new Format($name, $mediaTypes, $config['priority']);
43
    }
44
}
45