Completed
Push — master ( 4dab0f...442eb8 )
by Dmitry
02:31
created

Event::getEventInfo()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 0
cts 17
cp 0
rs 9.0534
cc 4
eloc 11
nc 4
nop 0
crap 20
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 index(Application $app, BasisEvent $event, Service $service)
13
    {
14
        try {
15
            $info = $this->getEventInfo();
16
            $subscription = $event->getSubscription();
17
18
            $patterns = [];
19
            foreach (array_keys($subscription) as $pattern) {
20
                if ($service->eventMatch($info->event, $pattern)) {
21
                    $patterns[] = $pattern;
22
                }
23
            }
24
25
            if (!count($patterns)) {
26
                $service->unsubscribe($info->event);
27
                throw new Exception("No subscription on event ".$info->event);
28
            }
29
30
            $listeners = [];
31
            foreach ($patterns as $pattern) {
32
                foreach ($subscription[$pattern] as $listener) {
33
                    if (!array_key_exists($listener, $listeners)) {
34
                        $listeners[$listener] = $app->get('Listener\\'.$listener);
35
                    }
36
                }
37
            }
38
39
            $result = [];
40
            $issues = [];
41
            foreach ($listeners as $nick => $listener) {
42
                $result[$nick] = $this->handleEvent($app, $listener, $info);
43
                try {
44
                    $event->fireChanges($nick);
45
                } catch (Exception $e) {
46
                    $issues[$nick] =  $e->getMessage();
47
                }
48
            }
49
50
51
            return [
52
                'success' => true,
53
                'data' => $result,
54
                'issues' => $issues,
55
            ];
56
57
        } catch (Exception $e) {
58
            return ['success' => false, 'message' => $e->getMessage()];
59
        }
60
    }
61
62
    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...
63
    {
64
        if (!array_key_exists('event', $_REQUEST)) {
65
            throw new Exception('No event defined');
66
        }
67
68
        if (!array_key_exists('context', $_REQUEST)) {
69
            throw new Exception('No context defined');
70
        }
71
72
        $context = json_decode($_REQUEST['context']);
73
74
        if (!$context) {
75
            throw new Exception('Invalid context');
76
        }
77
78
        return [
79
            'event' => $_REQUEST['event'],
80
            'context' => $context,
81
        ];
82
    }
83
84
    private function handleEvent($app, $instance, $info)
85
    {
86
        foreach ($info as $k => $v) {
87
            $instance->$k = $v;
88
        }
89
        if (!method_exists($instance, 'run')) {
90
            throw new Exception('No run method for '.$class);
91
        }
92
93
        return $app->call([$instance, 'run']);
94
    }
95
}
96