Passed
Push — master ( 4c2b59...061776 )
by Shahrad
02:09
created

PluginTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 26
c 1
b 0
f 0
dl 0
loc 85
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __execute() 0 18 5
A __identify() 0 9 2
A __checkExit() 0 13 6
1
<?php
2
declare(strict_types=1);
3
4
namespace TelegramBot\Traits;
5
6
use TelegramBot\Entities\Response;
7
use TelegramBot\Entities\Update;
8
use TelegramBot\UpdateHandler;
9
10
/**
11
 * PluginTrait class
12
 *
13
 * @link    https://github.com/telegram-bot-php/core
14
 * @author  Shahrad Elahi (https://github.com/shahradelahi)
15
 * @license https://github.com/telegram-bot-php/core/blob/master/LICENSE (MIT License)
16
 */
17
trait PluginTrait
18
{
19
20
    /**
21
     * @var UpdateHandler
22
     */
23
    protected UpdateHandler $hook;
24
25
    /**
26
     * @var \Generator
27
     */
28
    private \Generator $returns;
29
30
    /**
31
     * This property is used to kill the plugin when you yield a Response object.
32
     *
33
     * @var bool
34
     */
35
    protected bool $KILL_ON_YIELD = true;
36
37
    /**
38
     * Execute the plugin.
39
     *
40
     * @param UpdateHandler $receiver
41
     * @param Update $update
42
     * @return void
43
     */
44
    public function __execute(UpdateHandler $receiver, Update $update): void
45
    {
46
        $this->hook = $receiver;
47
48
        if (method_exists($this, '__process')) {
49
            $this->__process($update);
50
        }
51
52
        if (method_exists($this, 'onReceivedUpdate')) {
53
            $return = $this->onReceivedUpdate($update);
54
            $this->__checkExit($return);
55
        }
56
57
        $type = $this->__identify($update);
58
        if (method_exists($this, ($method = 'on' . $type)) && $type !== null) {
59
            $catchMethod = 'get' . ucfirst($type);
60
            $return = $this->$method($update->getUpdateId(), $update->$catchMethod());
61
            $this->__checkExit($return);
62
        }
63
    }
64
65
    /**
66
     * Check for the exit of the plugin.
67
     *
68
     * @param \Generator $return
69
     * @return void
70
     */
71
    private function __checkExit(\Generator $return): void
72
    {
73
        if ($return->valid()) {
74
            if ($return->current() !== null && $this->KILL_ON_YIELD === true) {
75
                if ($return->current() instanceof Response) {
76
                    $this->stop();
0 ignored issues
show
Bug introduced by
It seems like stop() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
                    $this->/** @scrutinizer ignore-call */ 
77
                           stop();
Loading history...
77
                }
78
            }
79
        }
80
81
        if ($return->valid()) {
82
            $return->next();
83
            $this->__checkExit($return);
84
        }
85
    }
86
87
    /**
88
     * Identify the update type. e.g. Message, EditedMessage, etc.
89
     *
90
     * @param Update $update
91
     * @return string|null
92
     */
93
    private function __identify(Update $update): ?string
94
    {
95
        $type = $update->getUpdateType();
96
97
        if ($type === null) {
98
            return null;
99
        }
100
101
        return str_replace('_', '', ucwords($type, '_'));
102
    }
103
104
}