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

Html   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
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 39
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A renderDice() 0 5 1
A contentType() 0 4 1
A urlPrefix() 0 4 1
A htmlForSingleDice() 0 10 3
1
<?php
2
3
namespace MeadSteve\DiceApi\Renderer;
4
5
use MeadSteve\DiceApi\Dice;
6
7
class Html implements DiceRenderer
8
{
9
    private $urlRoot;
10
11
    public function __construct($urlRoot)
12
    {
13
        $this->urlRoot = $urlRoot;
14
    }
15
16
    public function renderDice(array $diceCollection)
17
    {
18
        $diceHtmlParts = array_map([$this, 'htmlForSingleDice'], $diceCollection);
19
        return implode('', $diceHtmlParts);
20
    }
21
22
    /**
23
     * @return string
24
     */
25
    public function contentType() : string
26
    {
27
        return "text/html";
28
    }
29
30
    public function urlPrefix(): string
31
    {
32
        return "html";
33
    }
34
35
    public function htmlForSingleDice(Dice $dice)
36
    {
37
        $name = $dice->name();
38
        if ($name != "d6" && $name != "d20") {
39
            throw new UnrenderableDiceException("Currently only d6 and d20 can be rendered as html");
40
        }
41
        $roll = $dice->roll();
42
        $url = "{$this->urlRoot}/images/poorly-drawn/{$name}/{$roll}.png";
43
        return '<img src="' . $url . '" />';
44
    }
45
}
46