Completed
Push — feature/refactor-app-design ( a2f6ee...c863f7 )
by Avtandil
02:30
created

Kernel   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 95
ccs 0
cts 52
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C handle() 0 62 13
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
    public function __construct(Telegram $app)
36
    {
37
        $this->app = $app;
38
    }
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
    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
        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
        $offset = 0;
64
65
        // Take custom input into account.
66
        if ($custom_input = $this->app->getCustomInput()) {
67
            $response = new Response(json_decode($custom_input, true), $this->app->getBotUsername());
68
        } else {
69
            if (DB::isDbConnected()) {
70
                // Get last update id from the database
71
                $last_update = DB::selectTelegramUpdate(1);
72
                $last_update = reset($last_update);
73
74
                $this->app->last_update_id = isset($last_update['id']) ? $last_update['id'] : null;
75
            }
76
77
            if ($this->app->last_update_id !== null) {
78
                $offset = $this->app->last_update_id + 1;    //As explained in the telegram bot API documentation
79
            }
80
81
            $response = Client::getUpdates(
82
                [
83
                    'offset'  => $offset,
84
                    'limit'   => $limit,
85
                    'timeout' => $timeout,
86
                ]
87
            );
88
        }
89
90
        if ($response->isOk()) {
91
            $results = $response->getResult();
92
93
            // Process all updates
94
            /** @var \Longman\TelegramBot\Entities\Update $result */
95
            foreach ($results as $result) {
96
                $this->app->processUpdate($result);
97
            }
98
99
            if (! DB::isDbConnected() && ! $custom_input && $this->app->last_update_id !== null && $offset === 0) {
100
                // Mark update(s) as read after handling
101
                Client::getUpdates(
102
                    [
103
                        'offset'  => $this->app->last_update_id + 1,
104
                        'limit'   => 1,
105
                        'timeout' => $timeout,
106
                    ]
107
                );
108
            }
109
        }
110
111
        return $response;
112
    }
113
}
114