Client::getEventsHandler()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace erjanmx\nambaone;
4
5
use Closure;
6
use GuzzleHttp\Client as Guzzle;
7
use InvalidArgumentException;
8
9
class Client
10
{
11
    /**
12
     * @var Api 
13
     */
14
    public $api;
15
16
    /**
17
     * @var EventsHandler
18
     */
19
    protected $events_handler;
20
21
    /**
22
     * Client constructor
23
     * 
24
     * @param $token - api token
25
     * @param null $events_handler
26
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
27
    function __construct($token, $events_handler = null)
28
    {
29
        if (is_null($events_handler)) {
30
            $events_handler = new EventsHandler();
31
        } elseif (! $events_handler instanceof EventsHandler) {
32
            throw new InvalidArgumentException('Second parameter must be instance of EventsHandler');
33
        }
34
35
        $this->events_handler = $events_handler;
36
        $this->events_handler->setClient($this);
37
38
        $this->api = new Api($token);
39
    }
40
41
    /**
42
     * Missing methods go through Message
43
     * 
44
     * @param $name
45
     * @param $arguments
46
     * @return Message
47
     */
48
    public function __call($name, $arguments)
49
    {
50
        $message = new Message($this);
51
52
        call_user_func_array([$message, $name], $arguments);
53
54
        return $message;
55
    }
56
57
    /**
58
     * Assign event handler
59
     * 
60
     * @param $event
61
     * @param Closure $action
62
     * @return $this
63
     */
64
    public function command($event, Closure $action)
65
    {
66
        $this->events_handler->assign($event, $action);
67
68
        return $this;
69
    }
70
71
    /**
72
     * Handle incoming hooks from api 
73
     */
74
    public function handleEvents()
75
    {
76
        $post = json_decode(file_get_contents('php://input'), true);
77
78
        $this->events_handler->run($post['event'], $post['data']);
79
    }
80
81
    /**
82
     * @return EventsHandler
83
     */
84
    public function getEventsHandler()
85
    {
86
        return $this->events_handler;
87
    }
88
}
89