Completed
Push — master ( 33a6cb...e65a89 )
by Dmitry
09:53 queued 02:57
created

Event::__process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 3
crap 2
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
            foreach ($listeners as $nick => $listener) {
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()
0 ignored issues
show
Coding Style introduced by
getEventInfo uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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