Test Setup Failed
Push — develop ( 01f508...e1be97 )
by Mattias
01:57
created

APIPollClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace TelegramBot;
4
5
use TelegramBot\APIMessage;
6
use React\HttpClient\Client;
7
use React\HttpClient\Request;
8
9
class APIPollClient
10
{
11
    /** @var string */
12
  private $botToken;
13
  /** @var int */
14
  private $offset;
15
  /** @var \React\HttpClient\Client */
16
  private $client;
17
18
  /**
19
   * @param string $botToken
20
   */
21
  public function __construct(string $botToken, Client $client)
22
  {
23
      $this->botToken = $botToken;
24
      $this->client = $client;
25
  }
26
27
  /**
28
   * @param CallableInterface $responseInterface
0 ignored issues
show
Bug introduced by
There is no parameter named $responseInterface. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
29
   */
30
  public function poll($responseHandler)
31
  {
32
      $request = $this->client->request(
33
      'GET',
34
      $this->botCommand('getUpdates', ['offset' => $this->offset])
35
    );
36
      $request->on('response', $responseHandler);
37
      $request->on('error', function ($data) {
38
          throw new \Exception($data);
39
      });
40
      $request->end();
41
  }
42
43
  /**
44
   * @param string $answer
45
   * @param APIMessage $message
46
   */
47
  public function send($answer, APIMessage $message)
48
  {
49
      $responseData = $this->postDataEncoder($message->getResponseData($answer));
50
51
      $responseCall = $this->client->request(
52
      'POST',
53
      $this->botCommand('sendMessage'),
54
      $this->getResponseHeaders($responseData)
55
    );
56
57
      $responseCall->end($responseData);
58
  }
59
60
    public function markMessageHandled(APIMessage $message)
61
    {
62
        $this->offset = $message->getUpdateId() +1;
63
    }
64
65
    public function getResponseHeaders(string $responseString) :array
66
    {
67
        return [
68
      'Content-Type' =>  'application/x-www-form-urlencoded',
69
      'Content-Length' => strlen($responseString)
70
    ];
71
    }
72
73
74
    private function botCommand(string $command, array $params = []) :string
75
    {
76
        $params = (count($params)) ? '?' . $this->postDataEncoder($params) : '';
77
78
        return $this->assembleUri($command, $params);
79
    }
80
81
82
    private function assembleUri($command, $params) :string
83
    {
84
        return sprintf(
85
      'https://api.telegram.org/bot%s/%s%s',
86
      $this->botToken,
87
      $command,
88
      $params
89
    );
90
    }
91
92
    private function postDataEncoder(array $data) :string
93
    {
94
        $string = '';
95
96
        foreach ($data as $k => $v) {
97
            $string .= $k . '=' . urlencode($v) . '&';
98
        }
99
100
        return $string;
101
    }
102
}
103