Completed
Push — master ( 12ea9a...ae2bec )
by Steve
01:53
created

RendererCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 47
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A newForAcceptType() 0 9 3
A contentTypesForPaths() 0 8 2
1
<?php
2
3
namespace MeadSteve\DiceApi\Renderer;
4
5
class RendererCollection
6
{
7
    /**
8
     * @var DiceRenderer[]
9
     */
10
    private $renderers;
11
12
    /**
13
     * RendererCollection constructor.
14
     * @param DiceRenderer[] $renderers
15
     */
16
    public function __construct(array $renderers)
17
    {
18
        foreach ($renderers as $renderer) {
19
            $this->renderers[$renderer->contentType()] = $renderer;
20
        }
21
    }
22
23
    /**
24
     * @param string $acceptTypes
25
     * @return DiceRenderer
26
     * @throws UnknownRendererException
27
     */
28
    public function newForAcceptType($acceptTypes)
29
    {
30
        foreach (explode(",", $acceptTypes) as $acceptType) {
31
            if (array_key_exists($acceptType, $this->renderers)) {
32
                return $this->renderers[$acceptType];
33
            }
34
        }
35
        throw new UnknownRendererException;
36
    }
37
38
    /**
39
     * Returns key value pairs mapping a url prefix to a handled content type
40
     * so ["json" => "application/json", "etc" => "..."]
41
     * @return string[]
42
     */
43
    public function contentTypesForPaths() : array
44
    {
45
        $types = [];
46
        foreach ($this->renderers as $renderer) {
47
            $types[$renderer->urlPrefix()] = $renderer->contentType();
48
        }
49
        return $types;
50
    }
51
}
52