Event   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 80.38%

Importance

Changes 0
Metric Value
wmc 18
lcom 0
cbo 3
dl 0
loc 99
ccs 41
cts 51
cp 0.8038
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __process() 0 4 1
A getEventInfo() 0 26 4
A handleEvent() 0 11 3
C index() 0 52 10
1
<?php
2
3
namespace Basis\Controller;
4
5
use Basis\Application;
6
use Basis\Event as BasisEvent;
7
use Basis\Service;
8
use Exception;
9
10
class Event
11
{
12
    public function __process(Application $app, BasisEvent $event, Service $service)
13
    {
14
        return $this->index($app, $event, $service);
15
    }
16
17 1
    public function index(Application $app, BasisEvent $event, Service $service)
18
    {
19 1
        $start = microtime(1);
20
21
        try {
22 1
            $info = $this->getEventInfo();
23 1
            $subscription = $event->getSubscription();
24
25 1
            $patterns = [];
26 1
            foreach (array_keys($subscription) as $pattern) {
27 1
                if ($service->eventMatch($info->event, $pattern)) {
28 1
                    $patterns[] = $pattern;
29
                }
30
            }
31
32 1
            if (!count($patterns)) {
33
                $service->unsubscribe($info->event);
34
                throw new Exception("No subscription on event ".$info->event);
35
            }
36
37 1
            $listeners = [];
38 1
            foreach ($patterns as $pattern) {
39 1
                foreach ($subscription[$pattern] as $listener) {
40 1
                    if (!array_key_exists($listener, $listeners)) {
41 1
                        $listeners[$listener] = $app->get('Listener\\'.$listener);
42
                    }
43
                }
44
            }
45
46 1
            $result = [];
47 1
            $issues = [];
48 1
            foreach ($listeners as $nick => $listener) {
49 1
                $result[$nick] = $this->handleEvent($app, $listener, $info);
50
                try {
51 1
                    $event->fireChanges($nick);
52 1
                } catch (Exception $e) {
53 1
                    $issues[$nick] =  $e->getMessage();
54
                }
55
            }
56
57
58
            return [
59 1
                'success' => true,
60 1
                'data' => $result,
61 1
                'issues' => $issues,
62 1
                'time' => microtime(1) - $start,
63
            ];
64
65
        } catch (Exception $e) {
66
            return ['success' => false, 'message' => $e->getMessage()];
67
        }
68
    }
69
70 1
    private function getEventInfo()
71
    {
72 1
        if (!array_key_exists('event', $_REQUEST)) {
73
            throw new Exception('No event defined');
74
        }
75
76 1
        if (!array_key_exists('context', $_REQUEST)) {
77
            throw new Exception('No context defined');
78
        }
79
80 1
        $context = json_decode($_REQUEST['context']);
81
82 1
        if (!$context) {
83
            throw new Exception('Invalid context');
84
        }
85
86 1
        $parts = explode('.', $_REQUEST['event']);
87 1
        $action = array_pop($parts);
88
89
        return (object) [
90 1
            'event' => $_REQUEST['event'],
91 1
            'space' => implode('.', $parts),
92 1
            'action' => $action,
93 1
            'context' => $context,
94
        ];
95
    }
96
97 1
    private function handleEvent($app, $instance, $info)
98
    {
99 1
        foreach ($info as $k => $v) {
100 1
            $instance->$k = $v;
101
        }
102 1
        if (!method_exists($instance, 'run')) {
103
            throw new Exception('No run method for '.$class);
104
        }
105
106 1
        return $app->call([$instance, 'run']);
107
    }
108
}
109