Test Setup Failed
Push — master ( 584547...730ac2 )
by Alexey
14:04
created

WebHookController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B receiveUpdateAction() 0 32 4
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Controller\Telegram;
4
5
use Skobkin\Bundle\PointToolsBundle\Service\Telegram\IncomingUpdateDispatcher;
6
use Symfony\Bridge\Monolog\Logger;
7
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8
use Symfony\Component\HttpFoundation\{JsonResponse, Request, Response};
9
use unreal4u\TelegramAPI\Telegram\Types\Update;
10
11
/**
12
 * {@inheritdoc}
13
 */
14
class WebHookController extends AbstractController
15
{
16
    /** @var string */
17
    private $telegramToken;
18
19
    /** @var bool */
20
    private $debug;
21
22
    public function __construct(string $telegramToken, bool $debug)
23
    {
24
        $this->telegramToken = $telegramToken;
25
        $this->debug = $debug;
26
    }
27
28
    public function receiveUpdateAction(Request $request, string $token, IncomingUpdateDispatcher $updateDispatcher, Logger $logger): Response
29
    {
30
        if ($token !== $savedToken = $this->telegramToken) {
31
            throw $this->createNotFoundException();
32
        }
33
34
        $content = json_decode($request->getContent(), true);
35
36
        $update = new Update(
37
            $content,
38
            $logger
39
        );
40
41
        try {
42
            $updateDispatcher->process($update);
43
        } catch (\Exception $e) {
44
            if ($this->debug) {
45
                throw $e;
46
            }
47
48
            $logger->addError('Telegram bot error', [
49
                'exception' => get_class($e),
50
                'file' => $e->getFile(),
51
                'line' => $e->getLine(),
52
                'code' => $e->getCode(),
53
                'message' => $e->getMessage(),
54
                'trace' => $e->getTraceAsString(),
55
            ]);
56
        }
57
58
        return new JsonResponse('received');
59
    }
60
}
61