WebHookController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A receiveUpdateAction() 0 32 4
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Controller\Telegram;
4
5
use Psr\Log\LoggerInterface;
6
use Skobkin\Bundle\PointToolsBundle\Service\Telegram\IncomingUpdateDispatcher;
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, LoggerInterface $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', [
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Log\LoggerInterface as the method addError() does only exist in the following implementations of said interface: Monolog\Logger, Symfony\Bridge\Monolog\Logger.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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