Passed
Branch 3.0.0 (98e096)
by Pieter
02:47
created

ChainedFormatRetriever::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
4
namespace W2w\Lib\Apie\Encodings;
5
6
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
7
8
class ChainedFormatRetriever implements FormatRetrieverInterface
9
{
10
    /**
11
     * @var FormatRetrieverInterface[]
12
     */
13
    private $formatRetrievers;
14
15
    /**
16
     * @param FormatRetrieverInterface[]
17
     */
18
    public function __construct(array $formatRetrievers)
19
    {
20
        $this->formatRetrievers = $formatRetrievers;
21
    }
22
23
    /**
24
     * @param string $contentType
25
     * @return string|null
26
     */
27
    public function getFormat(string $contentType): ?string
28
    {
29
        foreach ($this->formatRetrievers as $formatRetriever) {
30
            $res = $formatRetriever->getFormat($contentType);
31
            if (!is_null($res)) {
32
                return $res;
33
            }
34
        }
35
        throw new NotAcceptableHttpException('"' . $contentType . '" is not accepted');
36
    }
37
38
    /**
39
     * @param string $format
40
     * @return string|null
41
     */
42
    public function getContentType(string $format): ?string
43
    {
44
        foreach ($this->formatRetrievers as $formatRetriever) {
45
            $res = $formatRetriever->getContentType($format);
46
            if (!is_null($res)) {
47
                return $res;
48
            }
49
        }
50
        throw new NotAcceptableHttpException('"' . $format . '" is not accepted');
51
    }
52
}
53