Completed
Push — master ( f47f8d...8687c2 )
by Steve
01:40
created

DiceApp   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 6
dl 0
loc 86
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A buildIndex() 0 9 1
A index() 0 10 2
A diceStats() 0 6 1
A healthCheck() 0 4 1
A setupRoutes() 0 13 2
A addCustomRoute() 0 14 1
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 INDEX_FILE_PATH = __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::INDEX_FILE_PATH, $indexContent);
42
        return $indexContent;
43
    }
44
45
    public function index(Request $request, Response $response)
46
    {
47
        if (!file_exists(self::INDEX_FILE_PATH)) {
48
            $indexContent = self::buildIndex();
49
        } else {
50
            $indexContent = file_get_contents(self::INDEX_FILE_PATH);
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
    {
65
        return $response->write("ok");
66
    }
67
68
    private function setupRoutes()
69
    {
70
        $diceRequestHandler = $this->diceRequestHandler;
71
72
        $this->get("/", [$this, 'index']);
73
        $this->get("/dice-stats", [$this, 'diceStats']);
74
        $this->get("/health-check", [$this, 'healthCheck']);
75
        $this->get(self::DICE_PATH_REGEX, [$diceRequestHandler, 'getDice']);
76
77
        foreach ($this->diceRequestHandler->contentTypesForPaths() as $path => $contentType) {
78
            $this->addCustomRoute($path, $contentType);
79
        }
80
    }
81
82
    private function addCustomRoute(string $path, string $contentType)
83
    {
84
        $diceRequestHandler = $this->diceRequestHandler;
85
        $this->get(
86
            "/{$path}" . self::DICE_PATH_REGEX,
87
            function (Request $request, $response, $args) use ($diceRequestHandler, $contentType) {
88
                return $diceRequestHandler->getDice(
89
                    $request->withHeader('accept', $contentType),
90
                    $response,
91
                    $args
92
                );
93
            }
94
        );
95
    }
96
}
97