Completed
Push — master ( 4967cd...fefc41 )
by Dmitry
16:49
created

src/Controller/Event.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    public function index(Application $app, BasisEvent $event, Service $service)
18
    {
19
        $start = microtime(1);
20
21
        try {
22
            $info = $this->getEventInfo();
23
            $subscription = $event->getSubscription();
24
25
            $patterns = [];
26
            foreach (array_keys($subscription) as $pattern) {
27
                if ($service->eventMatch($info->event, $pattern)) {
28
                    $patterns[] = $pattern;
29
                }
30
            }
31
32
            if (!count($patterns)) {
33
                $service->unsubscribe($info->event);
34
                throw new Exception("No subscription on event ".$info->event);
35
            }
36
37
            $listeners = [];
38
            foreach ($patterns as $pattern) {
39
                foreach ($subscription[$pattern] as $listener) {
40
                    if (!array_key_exists($listener, $listeners)) {
41
                        $listeners[$listener] = $app->get('Listener\\'.$listener);
42
                    }
43
                }
44
            }
45
46
            $result = [];
47
            $issues = [];
48 View Code Duplication
            foreach ($listeners as $nick => $listener) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
                $result[$nick] = $this->handleEvent($app, $listener, $info);
50
                try {
51
                    $event->fireChanges($nick);
52
                } catch (Exception $e) {
53
                    $issues[$nick] =  $e->getMessage();
54
                }
55
            }
56
57
58
            return [
59
                'success' => true,
60
                'data' => $result,
61
                'issues' => $issues,
62
                'time' => microtime(1) - $start,
63
            ];
64
65
        } catch (Exception $e) {
66
            return ['success' => false, 'message' => $e->getMessage()];
67
        }
68
    }
69
70
    private function getEventInfo()
71
    {
72
        if (!array_key_exists('event', $_REQUEST)) {
73
            throw new Exception('No event defined');
74
        }
75
76
        if (!array_key_exists('context', $_REQUEST)) {
77
            throw new Exception('No context defined');
78
        }
79
80
        $context = json_decode($_REQUEST['context']);
81
82
        if (!$context) {
83
            throw new Exception('Invalid context');
84
        }
85
86
        return (object) [
87
            'event' => $_REQUEST['event'],
88
            'context' => $context,
89
        ];
90
    }
91
92
    private function handleEvent($app, $instance, $info)
93
    {
94
        foreach ($info as $k => $v) {
95
            $instance->$k = $v;
96
        }
97
        if (!method_exists($instance, 'run')) {
98
            throw new Exception('No run method for '.$class);
99
        }
100
101
        return $app->call([$instance, 'run']);
102
    }
103
}
104