PluginTrait::__checkExit()   A
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
rs 9.2222
cc 6
nc 8
nop 1
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
    * @var UpdateHandler
21
    */
22
   protected UpdateHandler $hook;
23
24
   /**
25
    * This property is used to kill the plugin when you yield a Response object.
26
    *
27
    * @var bool
28
    */
29
   protected bool $KILL_ON_YIELD = true;
30
31
   /**
32
    * @var \Generator
33
    */
34
   private \Generator $returns;
35
36
   /**
37
    * Execute the plugin.
38
    *
39
    * @param UpdateHandler $receiver
40
    * @param Update $update
41
    * @return void
42
    */
43
   public function __execute(UpdateHandler $receiver, Update $update): void {
44
      $this->hook = $receiver;
45
46
      if (method_exists($this, '__process')) {
47
         $this->__process($update);
48
      }
49
50
      if (method_exists($this, 'onReceivedUpdate')) {
51
         $return = $this->onUpdate($update);
0 ignored issues
show
Bug introduced by
It seems like onUpdate() 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

51
         /** @scrutinizer ignore-call */ 
52
         $return = $this->onUpdate($update);
Loading history...
52
         $this->__checkExit($return);
53
      }
54
55
      $type = $this->__identify($update);
56
      if (method_exists($this, ($method = 'on' . $type)) && $type !== null) {
57
         $this->__checkExit($this->__callEvent($method, $update));
58
      }
59
   }
60
61
   /**
62
    * Check for the exit of the plugin.
63
    *
64
    * @param \Generator $return
65
    * @return void
66
    */
67
   private function __checkExit(\Generator $return): void {
68
      if ($return->valid()) {
69
         if ($return->current() !== null && $this->KILL_ON_YIELD === true) {
70
            if ($return->current() instanceof Response) {
71
               $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

71
               $this->/** @scrutinizer ignore-call */ 
72
                      stop();
Loading history...
72
            }
73
         }
74
      }
75
76
      if ($return->valid()) {
77
         $return->next();
78
         $this->__checkExit($return);
79
      }
80
   }
81
82
   /**
83
    * Identify the update type. e.g. Message, EditedMessage, etc.
84
    *
85
    * @param Update $update
86
    * @return string|null
87
    */
88
   private function __identify(Update $update): ?string {
89
      $type = $update->getUpdateType();
90
91
      if ($type === null) {
92
         return null;
93
      }
94
95
      return str_replace('_', '', ucwords($type, '_'));
96
   }
97
98
   /**
99
    * Pass data to the method.
100
    *
101
    * @param string $method The method name.
102
    * @param Update $update The update object.
103
    * @return \Generator
104
    */
105
   private function __callEvent(string $method, Update $update): \Generator {
106
      $upperName = 'get' . ucfirst(substr($method, 2));
107
      return match ($method) {
108
         'onWebAppData' => $this->onWebAppData($update->getWebAppData()),
0 ignored issues
show
Bug introduced by
It seems like onWebAppData() 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

108
         'onWebAppData' => $this->/** @scrutinizer ignore-call */ onWebAppData($update->getWebAppData()),
Loading history...
109
         default => $this->$method($update->getUpdateId(), $update->$upperName()),
110
      };
111
   }
112
113
}