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

Json   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 35
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renderDice() 0 8 1
A contentType() 0 4 1
A urlPrefix() 0 4 1
A diceAsAssocArrays() 0 9 1
1
<?php
2
3
namespace MeadSteve\DiceApi\Renderer;
4
5
use MeadSteve\DiceApi\Dice;
6
7
class Json implements DiceRenderer
8
{
9
10
    public function renderDice(array $diceCollection)
11
    {
12
        $data = [
13
            "success" => true,
14
            "dice" => $this->diceAsAssocArrays($diceCollection)
15
        ];
16
        return json_encode($data);
17
    }
18
19
    /**
20
     * @return string
21
     */
22
    public function contentType() : string
23
    {
24
        return "application/json";
25
    }
26
27
    public function urlPrefix(): string
28
    {
29
        return "json";
30
    }
31
32
    private function diceAsAssocArrays(array $diceCollection)
33
    {
34
        return array_map(function (Dice $dice) {
35
            return [
36
                "value" => $dice->roll(),
37
                "type"  => $dice->name()
38
            ];
39
        }, $diceCollection);
40
    }
41
}
42