Completed
Pull Request — master (#17)
by Steve
02:09
created

DiceApp::healthCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
namespace MeadSteve\DiceApi;
3
4
use League\CommonMark\CommonMarkConverter;
5
use MeadSteve\DiceApi\Counters\DiceCounter;
6
use MeadSteve\DiceApi\RequestHandler\DiceRequestHandler;
7
use Slim\App;
8
use Slim\Http\Request;
9
use Slim\Http\Response;
10
11
class DiceApp extends App
12
{
13
    private const indexFilePath = __DIR__ . "/generated-index.html";
14
    private $diceCounter;
15
    private $diceRequestHandler;
16
17
    const DICE_PATH_REGEX = "{dice:(?:/[0-9]*[dD][^\/]+)+/?}";
18
19
    public function __construct(DiceRequestHandler $diceRequestHandler, DiceCounter $diceCounter)
20
    {
21
        parent::__construct();
22
23
        $this->diceRequestHandler = $diceRequestHandler;
24
        $this->diceCounter = $diceCounter;
25
26
        $this->setupRoutes();
27
    }
28
29
    /**
30
     * Builds a nice index file from the repo's README.md
31
     * Saves it and returns it.
32
     *
33
     * @return string
34
     */
35
    public static function buildIndex()
36
    {
37
        $converter = new CommonMarkConverter();
38
        $indexBody = $converter->convertToHtml(file_get_contents(__DIR__ . "/../README.md"));
39
        $indexContent = file_get_contents(__DIR__ . "/../www/templates/index.html");
40
        $indexContent = str_replace("{{body}}", $indexBody, $indexContent);
41
        file_put_contents(self::indexFilePath, $indexContent);
42
        return $indexContent;
43
    }
44
45
    public function index(Request $request, Response $response)
46
    {
47
        if (!file_exists(self::indexFilePath)) {
48
            $indexContent = self::buildIndex();
49
        } else {
50
            $indexContent = file_get_contents(self::indexFilePath);
51
        }
52
        $response->write($indexContent);
53
        return $response;
54
    }
55
56
    public function diceStats(Request $request, Response $response)
57
    {
58
        $countData = $this->diceCounter->getCounts();
59
        return $response->write(json_encode($countData))
60
            ->withHeader("Content-Type", "application/json");
61
    }
62
63
    public function healthCheck(Request $request, Response $response) {
64
        return $response->write("ok");
65
    }
66
67
    private function setupRoutes()
68
    {
69
        $diceRequestHandler = $this->diceRequestHandler;
70
71
        $this->get("/", [$this, 'index']);
72
        $this->get("/dice-stats", [$this, 'diceStats']);
73
        $this->get("/health-check", [$this, 'healthCheck']);
74
        $this->get(self::DICE_PATH_REGEX, [$diceRequestHandler, 'getDice']);
75
76
        foreach ($this->diceRequestHandler->contentTypesForPaths() as $path => $contentType) {
77
            $this->addCustomRoute($path, $contentType);
78
        }
79
    }
80
81
    private function addCustomRoute(string $path, string $contentType)
82
    {
83
        $diceRequestHandler = $this->diceRequestHandler;
84
        $this->get(
85
            "/{$path}" . self::DICE_PATH_REGEX,
86
            function (Request $request, $response, $args) use ($diceRequestHandler, $contentType) {
87
                return $diceRequestHandler->getDice(
88
                    $request->withHeader('accept', $contentType),
89
                    $response,
90
                    $args
91
                );
92
            }
93
        );
94
    }
95
}
96