Completed
Pull Request — master (#16)
by Steve
03:28
created

Protobuf::urlPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace MeadSteve\DiceApi\Renderer;
4
5
use MeadSteve\DiceApi\Dice;
6
use MeadSteve\DiceApi\Protos\V1\Response;
7
8
class Protobuf implements DiceRenderer
9
{
10
11
    /**
12
     * @param Dice[] $diceCollection
13
     * @return string
14
     */
15
    public function renderDice(array $diceCollection)
16
    {
17
        $roll = new Response();
18
        $roll->setDice($this->diceAsProtos($diceCollection));
19
        return $roll->serializeToString();
20
    }
21
    /**
22
     * @return string
23
     */
24
    public function contentType() : string
25
    {
26
        return "application/x-protobuf";
27
    }
28
29
    public function urlPrefix(): string
30
    {
31
        return "protobuf";
32
    }
33
34
    private function diceAsProtos(array $diceCollection)
35
    {
36
        return array_map(function (Dice $dice) {
37
            $proto = new \MeadSteve\DiceApi\Protos\V1\Dice();
38
            $proto->setValue($dice->roll());
39
            $proto->setName($dice->name());
40
            return $proto;
41
        }, $diceCollection);
42
    }
43
}
44