DemoController::encode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 17
rs 9.9
1
<?php
2
3
namespace Pgs\HashIdBundle\Controller;
4
5
use Pgs\HashIdBundle\Annotation\Hash;
6
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\Routing\Annotation\Route;
10
11
/**
12
 * @Route('/hash-id/demo')
13
 */
14
class DemoController extends AbstractController
15
{
16
    /**
17
     * @Route("/encode", requirements={"id"="\d+"})
18
     *
19
     * @param int $id
20
     */
21
    public function encode($id): Response
22
    {
23
        $other = 30;
24
        $url1 = $this->generateUrl('pgs_hash_id_demo_decode', ['id' => $id, 'other' => $other]);
25
        $url2 = $this->generateUrl('pgs_hash_id_demo_decode_more', ['id' => $id, 'other' => $other]);
26
27
        $response = <<<EOT
28
            <html>
29
                <body>
30
                Provided id: $id, other: $other <br />
31
                Url with encoded parameter: <a href="$url1">$url1</a><br />
32
                Another url with encoded more parameters: <a href="$url2">$url2</a><br />
33
                </body>
34
            </html>
35
EOT;
36
37
        return new Response($response);
38
    }
39
40
    /**
41
     * @Route("/decode/{id}/{other}")
42
     * @Hash("id")
43
     */
44
    public function decode(Request $request, int $id, int $other): Response
45
    {
46
        return new Response($this->getDecodeResponse($request, $id, $other));
47
    }
48
49
    /**
50
     * @Route("/decode_more/{id}/{other}")
51
     * @Hash({"id", "other"})
52
     */
53
    public function decodeMore(Request $request, int $id, int $other): Response
54
    {
55
        return new Response($this->getDecodeResponse($request, $id, $other));
56
    }
57
58
    private function getDecodeResponse(Request $request, int $id, int $other): string
59
    {
60
        $providedId = $this->getRouteParam($request, 'id');
61
        $providedOther = $this->getRouteParam($request, 'other');
62
63
        $response = <<<EOT
64
            <html>
65
                <body>
66
                Provided id: <b>$providedId</b>, other: <b>$providedOther</b><br />
67
                Decoded id: <b>$id</b>, other: <b>$other</b><br />
68
                </body>
69
            </html>
70
EOT;
71
72
        return $response;
73
    }
74
75
    private function getRouteParam(Request $request, $param)
76
    {
77
        return $request->attributes->get('_route_params')[$param];
78
    }
79
}
80