Completed
Push — feature/refactor-app-design ( 84f9ff...b18d21 )
by Avtandil
04:09
created

Kernel   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 68.97%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 90
ccs 20
cts 29
cp 0.6897
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C handle() 0 57 12
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot\Console;
12
13
use Longman\TelegramBot\DB;
14
use Longman\TelegramBot\Http\Client;
15
use Longman\TelegramBot\Http\Request;
16
use Longman\TelegramBot\Http\Response;
17
use Longman\TelegramBot\Telegram;
18
19
class Kernel
20
{
21
    /**
22
     * The application implementation.
23
     *
24
     * @var \Longman\TelegramBot\Telegram
25
     */
26
    protected $app;
27
28
    /**
29
     * Create a new HTTP kernel instance.
30
     *
31
     * @param  \Longman\TelegramBot\Telegram $app
32
     *
33
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
34
     */
35 1
    public function __construct(Telegram $app)
36
    {
37 1
        $this->app = $app;
38 1
    }
39
40
    /**
41
     * Handle an incoming HTTP request.
42
     *
43
     * @param  \Longman\TelegramBot\Http\Request $request
44
     *
45
     * @param null $limit
46
     * @param null $timeout
47
     * @return \Longman\TelegramBot\Http\Response
48
     *
49
     * @throws \Longman\TelegramBot\Exception\TelegramException
50
     */
51 1
    public function handle(Request $request, $limit = null, $timeout = null)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
52
    {
53 1
        if (! DB::isDbConnected() && ! $this->app->getupdates_without_database) {
54
            return new Response(
55
                [
56
                    'ok'          => false,
57
                    'description' => 'getUpdates needs MySQL connection! (This can be overridden - see documentation)',
58
                ],
59
                $this->app->getBotUsername()
60
            );
61
        }
62
63 1
        $offset = 0;
64
65 1
        if (DB::isDbConnected()) {
66
            // Get last update id from the database
67 1
            $last_update = DB::selectTelegramUpdate(1);
68 1
            $last_update = reset($last_update);
69
70 1
            $this->app->last_update_id = isset($last_update['id']) ? $last_update['id'] : null;
71
        }
72
73 1
        if ($this->app->last_update_id !== null) {
74
            $offset = $this->app->last_update_id + 1;    //As explained in the telegram bot API documentation
75
        }
76
77 1
        $response = Client::getUpdates(
78
            [
79 1
                'offset'  => $offset,
80 1
                'limit'   => $limit,
81 1
                'timeout' => $timeout,
82
            ]
83
        );
84
85 1
        if ($response->isOk()) {
86 1
            $results = $response->getResult();
87
88
            // Process all updates
89
            /** @var \Longman\TelegramBot\Entities\Update $result */
90 1
            foreach ($results as $result) {
91
                $this->app->processUpdate($result);
92
            }
93
94 1
            if (! DB::isDbConnected() && ! $custom_input && $this->app->last_update_id !== null && $offset === 0) {
0 ignored issues
show
Bug introduced by
The variable $custom_input does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
95
                // Mark update(s) as read after handling
96
                Client::getUpdates(
97
                    [
98
                        'offset'  => $this->app->last_update_id + 1,
99
                        'limit'   => 1,
100
                        'timeout' => $timeout,
101
                    ]
102
                );
103
            }
104
        }
105
106 1
        return $response;
107
    }
108
}
109