EventsHandler::setClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace erjanmx\nambaone;
4
5
use Closure;
6
7
class EventsHandler
8
{
9
    /**
10
     * List of events name and its handlers
11
     * 
12
     * @var array
13
     */
14
    public $events = [];
15
16
    /**
17
     * @var Client
18
     */
19
    protected $client;
20
21
    /**
22
     * Handle api hooks
23
     * 
24
     * @param $name
25
     * @param $data
26
     */
27
    public function run($name, $data)
28
    {
29
        if (! array_key_exists($name, $this->events))
30
            throw new \BadMethodCallException;
31
32
        if (is_callable($this->events[$name]))
33
            call_user_func($this->events[$name], $data);
34
        else
35
            call_user_func([$this, $this->events[$name]], $data);
36
    }
37
38
    /**
39
     * Assign api hook handler
40
     * 
41
     * @param $name
42
     * @param Closure $closure
43
     * @return $this
44
     */
45
    public function assign($name, Closure $closure)
46
    {
47
        $this->events[$name] = $closure;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param $client
54
     * @return $this
55
     */
56
    public function setClient($client)
57
    {
58
        $this->client = $client;
59
        
60
        return $this;
61
    }
62
}
63