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

DiceRequestHandler::contentTypesForPaths()   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 0
1
<?php
2
3
namespace MeadSteve\DiceApi\RequestHandler;
4
5
use MeadSteve\DiceApi\Counters\DiceCounter;
6
use MeadSteve\DiceApi\Dice;
7
use MeadSteve\DiceApi\UrlDiceGenerator;
8
use MeadSteve\DiceApi\Dice\UncreatableDiceException;
9
use MeadSteve\DiceApi\DiceDecorators\TotallyLegit;
10
use MeadSteve\DiceApi\Renderer\RendererCollection;
11
use MeadSteve\DiceApi\Renderer\UnknownRendererException;
12
use MeadSteve\DiceApi\Renderer\UnrenderableDiceException;
13
use Slim\Http\Request;
14
use Slim\Http\Response;
15
16
class DiceRequestHandler
17
{
18
19
    private $diceGenerator;
20
    private $diceCounter;
21
    private $rendererCollection;
22
23
    public function __construct(
24
        UrlDiceGenerator $diceGenerator,
25
        RendererCollection $rendererCollection,
26
        DiceCounter $diceCounter
27
    ) {
28
        $this->diceGenerator = $diceGenerator;
29
        $this->rendererCollection = $rendererCollection;
30
        $this->diceCounter = $diceCounter;
31
    }
32
33
    public function getDice(Request $request, Response $response, $args)
34
    {
35
        $diceResponse = $response->withHeader("cache-control", "no-cache");
36
        try {
37
            $dice = $this->diceGenerator->diceFromUrlString($args['dice']);
38
            if ($request->hasHeader('totally-legit')) {
39
                $dice = $this->makeDiceTotallyLegit($dice, $request);
40
            }
41
            $diceResponse = $this->writeAppropriateFormatResponse($request, $diceResponse, $dice);
42
            $this->diceCounter->count($dice);
43
        } catch (UncreatableDiceException $creationError) {
44
            $diceResponse = $diceResponse->withStatus(400)
45
                ->write("Unable to roll dice: " . $creationError->getMessage());
46
        } catch (UnrenderableDiceException $renderError) {
47
            $diceResponse = $diceResponse->withStatus(400)
48
                ->write("Unable to render request: " . $renderError->getMessage());
49
        }
50
        return $diceResponse;
51
    }
52
53
    /**
54
     * Returns key value pairs mapping a url prefix to a handled content type
55
     * so ["json" => "application/json", "etc" => "..."]
56
     * @return string[]
57
     */
58
    public function contentTypesForPaths() : array
59
    {
60
        return $this->rendererCollection->contentTypesForPaths();
61
    }
62
63
    private function writeAppropriateFormatResponse(Request $request, Response $response, $dice)
64
    {
65
        $acceptHeader = $request->getHeader('accept');
66
        $requestedContentType = $acceptHeader[0];
67
        try {
68
            $renderer = $this->rendererCollection->newForAcceptType($requestedContentType);
69
            $responseWithOutput = $response->write($renderer->renderDice($dice))
70
                ->withHeader("Content-Type", $renderer->contentType());
71
        } catch (UnknownRendererException $error) {
72
            $responseWithOutput = $response->withStatus(406);
73
            $responseWithOutput->write("Not sure how to respond with: " . $requestedContentType);
74
        }
75
        return $responseWithOutput;
76
    }
77
78
    private function makeDiceTotallyLegit($dice, Request $request)
79
    {
80
        $rolledValue = $request->getHeader('totally-legit')[0];
81
        return array_map(
82
            function (Dice $dice) use ($rolledValue) {
83
                return new TotallyLegit($dice, (int) $rolledValue);
84
            },
85
            $dice
86
        );
87
    }
88
}
89