Completed
Push — master ( 741d83...eb3c74 )
by Michał
02:22
created

Event::createObjectFromString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 2
rs 9.6666
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 14
    public function __construct(array $config, $log)
31
    {
32 14
        $this->log = $log;
33 14
        $this->config = $config;
34
35 14
        switch (true) {
36 14
            case $config['event_object'] instanceof EventDispatcherInterface:
37 4
                $this->event = $config['event_object'];
38 4
                break;
39
40 11
            case is_string($config['event_object']):
41 9
                $this->classExists($config['event_object'])
42 7
                    ->createObjectFromString($config);
43 5
                break;
44
45 3
            default:
46 3
                $this->eventObjectException($config['event_object']);
47 3
        }
48 8
    }
49
50
    /**
51
     * @param array $config
52
     * @throws \LogicException
53
     */
54 7
    protected function createObjectFromString(array $config)
55
    {
56 7
        $this->event = new $config['event_object']($config['event_config']);
57
58 7
        if (!$this->event instanceof EventDispatcherInterface) {
59 3
            $message = 'Event should be instance of ' . EventDispatcherInterface::class . ': '
60 3
                . get_class($this->event);
61 3
            $this->makeLog($message);
62 3
            throw new \LogicException($message);
63
        }
64 5
    }
65
66
    /**
67
     * @param string|object $eventObject
68
     * @throws \LogicException
69
     */
70 3
    protected function eventObjectException($eventObject)
71
    {
72 3
        $object = 'unknown type';
73
74 3
        if (is_object($eventObject)) {
75 3
            $object = get_class($eventObject);
76 3
        }
77
78 3
        $message = 'Cannot create Event instance: ' . $object;
79 3
        $this->makeLog($message);
80 3
        throw new \LogicException($message);
81
    }
82
83
    /**
84
     * check that class exists and throw exception if not
85
     *
86
     * @param string $namespace
87
     * @return $this
88
     * @throws \InvalidArgumentException
89
     */
90 9
    protected function classExists($namespace)
91
    {
92 9
        if (!class_exists($namespace)) {
93 2
            throw new \InvalidArgumentException('Class don\'t exists: ' . $namespace);
94
        }
95
96 7
        return $this;
97
    }
98
99
    /**
100
     * @param string|array $message
101
     * @return $this
102
     */
103 8
    public function makeLog($message)
104
    {
105 8
        if (!is_null($this->log)) {
106 5
            $this->log->makeLog($message);
107 5
        }
108
109 8
        return $this;
110
    }
111
112
    /**
113
     * @param string $name
114
     * @param array $data
115
     * @return $this
116
     */
117 4
    public function callEvent($name, array $data)
118
    {
119 4
        if (!is_null($this->event)) {
120 4
            $this->event->triggerEvent($name, $data + [$this->config]);
121 3
            $this->makeLog('Triggered: ' . $name);
122 3
        }
123
124 3
        return $this;
125
    }
126
}
127