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

ChainedFormatRetriever   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentType() 0 9 3
A getFormat() 0 9 3
A __construct() 0 3 1
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