Completed
Push — master ( fe0508...11cb26 )
by Kirill
02:20
created

TelegramController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 6
dl 0
loc 56
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setupWebhookAction() 0 18 3
A updateAction() 0 23 2
A getUpdateReceiverService() 0 5 1
1
<?php
2
/**
3
 * Buktopuha - Telegram bot for russian trivia game
4
 *
5
 * PHP Version 5
6
 *
7
 * @category Symfony
8
 * @package  Chrl_Buktopuha
9
 * @author   Kirill Kholodilin <[email protected]>
10
 * @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
11
 * @link     https://take2.ru/
12
 */
13
namespace Chrl\AppBundle\Controller;
14
15
use Chrl\AppBundle\Type\Update;
16
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
17
use Symfony\Component\Config\Definition\Exception\Exception;
18
use Symfony\Component\HttpFoundation\JsonResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
21
/**
22
 * Telegram controller - shows homepage and status
23
 *
24
 * @category Symfony
25
 * @package  Chrl_Buktopuha
26
 * @author   Kirill Kholodilin <[email protected]>
27
 * @license  http://www.gnu.org/copyleft/gpl.html MIT
28
 * @link     https://take2.ru/
29
 */
30
class TelegramController extends Controller
31
{
32
33
	public function setupWebhookAction($secret)
34
	{
35
		$api = $this->get("buktopuha.telegram_bot_api");
36
		$config = $this->getParameter("buktopuha.config");
37
38
		if (empty($config['webhook']['domain'])) {
39
			throw new Exception("buktopuha.webhook.domain' is not set in config.yml", 0);
40
		}
41
		$url = "https://" . $config['webhook']['domain'] . $config['webhook']['path_prefix'] . "/telegram-bot/update";
42
		if (null !== $secret) {
43
			$url .= "/" . $secret;
44
		}
45
		$res = $api->setwebhook($url);
46
		return new JsonResponse([
47
			'ok' => $res,
48
			'url' => $url
49
		]);
50
	}
51
52
	public function updateAction($secret, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $secret is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
53
	{
54
		$data0 = $request->getContent();
55
56
		$data = json_decode($data0);
57
58
		$api = $this->get("buktopuha.telegram_bot_api");
0 ignored issues
show
Unused Code introduced by
$api is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
59
		$config = $this->getParameter("buktopuha.config");
60
61
		if (empty($config['webhook']['update_receiver'])) {
62
			throw new Exception("'webhook.update_receiver' is not valud service name", 0);
63
		}
64
65
		$updateReceiver = $this->getUpdateReceiverService($config['webhook']['update_receiver']);
66
67
		$update = new Update($data);
68
69
		$updateReceiver->handleUpdate($update);
70
71
		return new JsonResponse([
72
			'ok' => true
73
		]);
74
	}
75
76
	/**
77
	 *
78
	 * @return UpdateReceiverInterface
79
	 */
80
	protected function getUpdateReceiverService($serviceName)
81
	{
82
		return $this->container
83
			->get($serviceName);
84
	}
85
}
86