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 |
36
|
|
|
->withHeader("cache-control", "no-cache") |
37
|
|
|
->withHeader('Access-Control-Allow-Origin', '*'); |
38
|
|
|
try { |
39
|
|
|
$diceCollection = $this->diceCollectionFromRequest($request, $args); |
40
|
|
|
$this->diceCounter->count($diceCollection); |
41
|
|
|
return $this->writeAppropriateFormatResponse($request, $diceResponse, $diceCollection); |
42
|
|
|
} catch (UncreatableDiceException $creationError) { |
43
|
|
|
return $diceResponse->withStatus(400) |
44
|
|
|
->write("Unable to roll diceCollection: " . $creationError->getMessage()); |
45
|
|
|
} catch (UnrenderableDiceException $renderError) { |
46
|
|
|
return $diceResponse->withStatus(400) |
47
|
|
|
->write("Unable to render request: " . $renderError->getMessage()); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns key value pairs mapping a url prefix to a handled content type |
53
|
|
|
* so ["json" => "application/json", "etc" => "..."] |
54
|
|
|
* @return string[] |
55
|
|
|
*/ |
56
|
|
|
public function contentTypesForPaths() : array |
57
|
|
|
{ |
58
|
|
|
return $this->rendererCollection->contentTypesForPaths(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Request $request |
63
|
|
|
* @param Response $response |
64
|
|
|
* @param Dice[] $diceCollection |
65
|
|
|
* @return Response |
66
|
|
|
*/ |
67
|
|
|
private function writeAppropriateFormatResponse(Request $request, Response $response, array $diceCollection) |
68
|
|
|
{ |
69
|
|
|
$acceptHeader = $request->getHeader('accept'); |
70
|
|
|
$requestedContentType = $acceptHeader[0]; |
71
|
|
|
try { |
72
|
|
|
$renderer = $this->rendererCollection->newForAcceptType($requestedContentType); |
73
|
|
|
$responseWithOutput = $response->write($renderer->renderDice($diceCollection)) |
74
|
|
|
->withHeader("Content-Type", $renderer->contentType()); |
75
|
|
|
} catch (UnknownRendererException $error) { |
76
|
|
|
$responseWithOutput = $response->withStatus(406); |
77
|
|
|
$responseWithOutput->write("Not sure how to respond with: " . $requestedContentType); |
78
|
|
|
} |
79
|
|
|
return $responseWithOutput; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Dice[] $diceCollection |
84
|
|
|
* @param Request $request |
85
|
|
|
* @return Dice[] |
86
|
|
|
*/ |
87
|
|
|
private function makeDiceTotallyLegit(array $diceCollection, Request $request) |
88
|
|
|
{ |
89
|
|
|
$rolledValue = $request->getHeader('totally-legit')[0]; |
90
|
|
|
return array_map( |
91
|
|
|
function (Dice $dice) use ($rolledValue) { |
92
|
|
|
return new TotallyLegit($dice, (int) $rolledValue); |
93
|
|
|
}, |
94
|
|
|
$diceCollection |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Request $request |
100
|
|
|
* @param mixed[] $args |
101
|
|
|
* @return Dice[] |
102
|
|
|
*/ |
103
|
|
|
private function diceCollectionFromRequest(Request $request, $args) |
104
|
|
|
{ |
105
|
|
|
$diceCollection = $this->diceGenerator->diceFromUrlString($args['diceCollection']); |
106
|
|
|
if ($request->hasHeader('totally-legit')) { |
107
|
|
|
return $this->makeDiceTotallyLegit($diceCollection, $request); |
108
|
|
|
} |
109
|
|
|
return $diceCollection; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|