Passed
Push — master ( 699016...741d83 )
by Michał
03:32
created

Event::callEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BlueRegister\Events;
4
5
use BlueEvent\Event\Base\Interfaces\EventDispatcherInterface;
6
7
class Event
8
{
9
    /**
10
     * @var \BlueEvent\Event\Base\Interfaces\EventDispatcherInterface
11
     */
12
    protected $event;
13
14
    /**
15
     * @var null|\BlueRegister\Log
16
     */
17
    protected $log;
18
19
    /**
20
     * @var array
21
     */
22
    protected $config = [];
23
24
    /**
25
     * @param array $config
26
     * @param \BlueRegister\Log $log
27
     * @throws \LogicException
28
     * @throws \InvalidArgumentException
29
     */
30 8
    public function __construct(array $config, $log)
31
    {
32 8
        $this->log = $log;
33 8
        $this->config = $config;
34
35 8
        switch (true) {
36 8
            case $config['event_object'] instanceof EventDispatcherInterface:
37 1
                $this->event = $config['event_object'];
38 1
                break;
39
40 7
            case is_string($config['event_object']) && $this->classExists($config['event_object']):
41 5
                $this->event = new $config['event_object']($config['event_config']);
42
43 5
                if (!$this->event instanceof EventDispatcherInterface) {
44 2
                    $message = 'Event should be instance of ' . EventDispatcherInterface::class . ': '
45 2
                        . get_class($this->event);
46 2
                    $this->makeLog($message);
47 2
                    throw new \LogicException($message);
48
                }
49
50 4
                break;
51
52 2
            default:
53 2
                $message = 'Cannot create Event instance: ' . get_class($config['event_object']);
54 2
                $this->makeLog($message);
55 2
                throw new \LogicException($message);
56
57
                break;
58 2
        }
59 5
    }
60
61
    /**
62
     * check that class exists and throw exception if not
63
     *
64
     * @param string $namespace
65
     * @return $this
66
     * @throws \InvalidArgumentException
67
     */
68 6
    protected function classExists($namespace)
69
    {
70 6
        if (!class_exists($namespace)) {
71 1
            throw new \InvalidArgumentException('Class don\'t exists: ' . $namespace);
72
        }
73
74 5
        return $this;
75
    }
76
77
    /**
78
     * @param string|array $message
79
     * @return $this
80
     */
81 4
    public function makeLog($message)
82
    {
83 4
        if (!is_null($this->log)) {
84 1
            $this->log->makeLog($message);
85 1
        }
86
87 4
        return $this;
88
    }
89
90
    /**
91
     * @param string $name
92
     * @param array $data
93
     * @return $this
94
     */
95 2
    public function callEvent($name, array $data)
96
    {
97 2
        if (!is_null($this->event)) {
98 2
            $this->event->triggerEvent($name, $data + [$this->config]);
99 1
            $this->makeLog('Triggered: ' . $name);
100 1
        }
101
102 1
        return $this;
103
    }
104
}
105