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

Html::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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