Completed
Push — master ( 95b8e4...b19727 )
by Dmitry
06:52
created

src/Controller/Event.php (1 issue)

super-globals are not used.

Coding Style Minor

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