EventCollection::add()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 3
c 3
b 1
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace TelegramBot\Api\Events;
4
5
use Closure;
6
use ReflectionFunction;
7
use TelegramBot\Api\Botan;
8
use TelegramBot\Api\Types\Update;
9
10
class EventCollection
11
{
12
    /**
13
     * Array of events.
14
     *
15
     * @var array
16
     */
17
    protected $events;
18
19
    /**
20
     * Botan tracker
21
     *
22
     * @var \TelegramBot\Api\Botan|null
23
     */
24
    protected $tracker;
25
26
    /**
27
     * EventCollection constructor.
28
     *
29
     * @param string $trackerToken
30
     */
31 13
    public function __construct($trackerToken = null)
32
    {
33 13
        $this->events = [];
34 2
        if ($trackerToken) {
35 2
            @trigger_error(sprintf('Passing $trackerToken to %s is deprecated', self::class), \E_USER_DEPRECATED);
36 13
            $this->tracker = new Botan($trackerToken);
0 ignored issues
show
Deprecated Code introduced by
The class TelegramBot\Api\Botan has been deprecated. ( Ignorable by Annotation )

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

36
            $this->tracker = /** @scrutinizer ignore-deprecated */ new Botan($trackerToken);
Loading history...
37
        }
38
    }
39
40
    /**
41
     * Add new event to collection
42
     *
43
     * @param Closure $event
44
     * @param Closure|null $checker
45
     *
46
     * @return \TelegramBot\Api\Events\EventCollection
47 4
     */
48
    public function add(Closure $event, $checker = null)
49 4
    {
50 1
        $this->events[] = !is_null($checker) ? new Event($event, $checker)
51 1
            : new Event($event, function () {});
52
53 4
        return $this;
54
    }
55
56
    /**
57
     * @return void
58
     */
59 2
    public function handle(Update $update)
60
    {
61 2
        foreach ($this->events as $event) {
62
            /* @var \TelegramBot\Api\Events\Event $event */
63 2
            if ($event->executeChecker($update) === true) {
64 2
                if ($event->executeAction($update) === false) {
65 1
                    if ($this->tracker && ($message = $update->getMessage())) {
66 1
                        $checker = new ReflectionFunction($event->getChecker());
67 1
                        $this->tracker->track($message, $checker->getStaticVariables()['name']);
68 1
                    }
69 1
                    break;
70
                }
71 1
            }
72 2
        }
73 2
    }
74
}
75