Passed
Pull Request — master (#408)
by Alexander
02:08 queued 28s
created

EventCollection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
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
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
        if ($trackerToken) {
34 2
            $this->tracker = new Botan($trackerToken);
35 2
        }
36 13
    }
37
38
39
    /**
40
     * Add new event to collection
41
     *
42
     * @param Closure $event
43
     * @param Closure|null $checker
44
     *
45
     * @return \TelegramBot\Api\Events\EventCollection
46
     */
47 4
    public function add(Closure $event, $checker = null)
48
    {
49 4
        $this->events[] = !is_null($checker) ? new Event($event, $checker)
50 1
            : new Event($event, function () {
51 1
            });
52
53 4
        return $this;
54
    }
55
56
    /**
57
     * @param \TelegramBot\Api\Types\Update
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 (false === $event->executeAction($update)) {
65 1
                    if (!is_null($this->tracker)) {
66 1
                        $checker = new ReflectionFunction($event->getChecker());
67 1
                        $this->tracker->track($update->getMessage(), $checker->getStaticVariables()['name']);
68 1
                    }
69 1
                    break;
70
                }
71 1
            }
72 2
        }
73 2
    }
74
}
75