Passed
Pull Request — master (#15)
by Takashi
02:54
created

DefaultController::webhook()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 4
nop 4
dl 0
loc 27
ccs 14
cts 14
cp 1
crap 5
rs 9.4555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Esa\HtmlHandler;
8
use App\Esa\Proxy;
9
use App\Esa\WebhookValidator;
10
use App\Service\AccessController;
11
use App\Service\AssetResolver;
12
use Polidog\Esa\Exception\ClientException;
13
use Psr\Log\LoggerInterface;
14
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15
use Symfony\Component\HttpFoundation\JsonResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19
use Symfony\Component\Routing\Annotation\Route;
20
21
#[Route('/', name: 'default_')]
22 1
class DefaultController extends AbstractController
23
{
24 1
    #[Route('/', name: 'index')]
25
    public function index(Request $request): Response
26
    {
27 1
        if ($postId = $request->get('post_id')) {
28 1
            return $this->redirectToRoute('default_post', ['id' => $postId]);
29
        }
30
31 1
        return $this->render('default/index.html.twig');
32
    }
33
34 1
    #[Route('/post/{id}', name: 'post', requirements: ['id' => '\d+'])]
35
    public function post(
36
        Request $request,
37
        int $id,
38
        Proxy $esa,
39
        AccessController $accessController,
40
        HtmlHandler $htmlHandler,
41
        AssetResolver $assetResolver,
42
        array $htmlReplacements,
43
    ): Response {
44 1
        $force = boolval($request->get('force', 0));
45
46
        try {
47 1
            $post = $esa->getPost($id, $force);
48
        } catch (ClientException $e) {
49
            throw new NotFoundHttpException();
50
        }
51
52 1
        if (!$accessController->isPublic($post['category'], $post['tags'])) {
53 1
            throw new NotFoundHttpException();
54
        }
55
56
        // fix body_html
57
        $htmlHandler
58 1
            ->initialize($post['body_html'])
59 1
            ->replacePostUrls('post', 'id')
60 1
            ->disableMentionLinks()
61 1
            ->replaceEmojiCodes()
62 1
            ->replaceHtml($htmlReplacements)
63 1
            ->dumpHtml()
64
        ;
65 1
        $post['body_html'] = $htmlHandler->dumpHtml();
66 1
        $toc = $htmlHandler->getToc();
67
68 1
        $assetPaths = $assetResolver->getAssetPaths($post['category'], $post['tags']);
69
70 1
        if ($force) {
71 1
            return $this->redirectToRoute('default_post', ['id' => $id]);
72
        }
73
74 1
        return $this->render('default/post.html.twig', [
75 1
            'post' => $post,
76 1
            'toc' => $toc,
77 1
            'css' => $assetPaths['css'],
78 1
            'js' => $assetPaths['js'],
79
        ]);
80
    }
81
82 1
    #[Route('/webhook', name: 'webhook', methods: ['POST'])]
83
    public function webhook(
84
        Request $request,
85
        WebhookValidator $validator,
86
        Proxy $esa,
87
        LoggerInterface $logger
88
    ): Response {
89 1
        $payload = $request->getContent();
90 1
        $signature = $request->headers->get('X-Esa-Signature');
91
92 1
        if ($signature && !$validator->isValid($payload, $signature)) {
93 1
            throw new NotFoundHttpException();
94
        }
95
96 1
        $body = json_decode($request->getContent(), true);
0 ignored issues
show
Bug introduced by
It seems like $request->getContent() can also be of type resource; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        $body = json_decode(/** @scrutinizer ignore-type */ $request->getContent(), true);
Loading history...
97
98 1
        switch ($body['kind']) {
99 1
            case 'post_create':
100 1
            case 'post_update':
101 1
                $esa->getPost($body['post']['number'], true);
102 1
                $logger->debug(sprintf('Cache for post %d is warmed up!', $body['post']['number']));
103 1
                break;
104
            default:
105 1
                break;
106
        }
107
108 1
        return new JsonResponse('OK');
109
    }
110
}
111