Passed
Branch 3.0.0 (0ebb76)
by Pieter
02:27
created

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