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

Kernel::handle()   C

Complexity

Conditions 12
Paths 19

Size

Total Lines 57
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 17.975

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 57
ccs 17
cts 26
cp 0.6538
rs 6.62
cc 12
eloc 27
nc 19
nop 3
crap 17.975

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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